-1

考えられる解決策の 1 つは、次のとおりです。AStart Time A(有効な Matlab 識別子でさえありません)などの名前は非常に混同しやすいため、変数に意味のある名前を自由に割り当てたことに注意してください。また、すべての情報が 、 、 およびで既にエンコードされているため、行列Cおよびが冗長であることがわかります 。Start Time CABStart Time A

% 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 マトリックスを解くために上記のコードを変更するにはどうすればよいですか。

例: 3 番目の次元のサイズは 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]

結果を出すためのコードの作り方。ありがとう

4

1 に答える 1

0

これにより、質問に完全に答えられない場合に質問に取り組む方法がわかります。私がここに入れているコードはテストしていません。提供されているものを私の知る限り変更しただけです。

result = zeros(size(start, 1), max_row_length, size(start,3)); 

% Finally fill the matrix using a double loop. 
for depth = 1:size(start,3)
    for row = 1 : size(start, 1) 
        for column = 1 : size(start, 2) 
            s = start(row, column, depth); 
            c = count(row, column, depth); 
            v = value(row, column, depth); 
            result(row, s : s + c - 1, depth) = v; 
        end 
    end 
end
于 2012-07-26T14:55:22.067 に答える