一种可能的解决方案如下。请注意,我自由地为变量指定了有意义的名称,因为名称如A
和Start Time A
(它甚至不是有效的Matlab标识符)很容易混淆。你也可以看到你的矩阵 C
和Start Time C
是多余的,因为所有的信息已经编码在A
, B
和Start Time A
。如何通过考虑speciall另一个矩阵输入值到矩阵
% The values to put in the result matrix.
value = [5 6 7;
7 5 6];
% Column index where each sequence starts in the result matrix.
start = [2 3 7;
1 6 8];
% The length of each sequence, i.e. how often to put the value into the result.
count = [1 2 3;
3 1 2];
% Determine the longest row. Note: At this place you could also check, if all
% rows are of the same length. The current implementation pads shorter rows with
% zeros.
max_row_length = max(start(:, end) + count(:, end) - 1);
% Allocate an output matrix filled with zeros. This avoids inserting sequences
% of zeros afterwards.
result = zeros(size(start, 1), max_row_length);
% Finally fill the matrix using a double loop.
for row = 1 : size(start, 1)
for column = 1 : size(start, 2)
s = start(row, column);
c = count(row, column);
v = value(row, column);
result(row, s : s + c - 1) = v;
end
end
的result
是
result =
0 5 6 6 0 0 7 7 7
7 7 7 0 0 5 0 6 6
的要求。
如何修改上述解决3D矩阵的代码。
例如:第三个维度的尺寸为2 矩阵值
value(:,:,1) = [5 6 7;
7 5 6];
value(:,:,2) = [6 5 7;
6 7 5];
start(:,:,1) = [2 3 7;
1 6 8];
start(:,:,2) = [1 5 6;
2 5 9];
count(:,:,1) = [1 2 3;
3 1 2];
count(:,:,2) = [2 1 3;
2 3 1];
我希望我的结果矩阵是
result(:,:,1) =[0 5 6 6 0 0 7 7 7;
7 7 7 0 0 5 0 6 6]
result(:,:,2) =[6 6 0 0 5 7 7 7 0;
0 6 6 0 7 7 7 0 5]
如何使代码以使结果。谢谢
您能否举一个例子说明如何使用“深度”信息将元素放置到3D矩阵中?这里用2D很简单。我主要想知道如何提供信息以知道将元素放置在3D中的位置以及结果的例子。我认为这可能就像添加另一个循环遍历你的第三维一样简单。 – 2012-07-26 14:03:19
@BenA。例如: – 2012-07-26 14:21:58
@BenA。先生看到我更新的问题。我举例说,第三维的大小是2.但实际上我有超过2. – 2012-07-26 14:37:16