フィルターのクリア

Fetching data to the matrix in a particular sequence

1 回表示 (過去 30 日間)
Bathrinath
Bathrinath 2013 年 12 月 30 日
コメント済み: Bathrinath 2013 年 12 月 31 日
m=3;
p=[5,8,10,11,6,7,9];
seq=[3,4,2,6,5,1,7];
out=[10,11,8;6,5,7;0,0,9];
Since m=3 I have to place all these values in three columns.
In 'out' matrix first row is filled by the seq of p array, then the second row of the 'out' matrix is filled by choosing of the minimum value from the first row (which is 8 and is placed in third column) so the sixth seq from p has to be be placed in column 3 of the second row of 'out' matrix.Next min value in first row of 'out' matrix is 10 which is placed in column 1 of 'out' matrix so the fifth seq from p has to be be placed in column 1 of the second row of 'out' matrix. In the second row of the 'out' matrix only one column is empty so move the first seq of p to that column. Now the remaining item in the seq is 7 which holds the p value as 9. This value has to be pushed to third row of 'out' matrix. Similar to second row of 'out' matrix minimum value has to be selected, but here two rows are in 'out' matrix now both the rows in 'out' matrix has to be added and min value which holds the column has to be selected and the remaining value has to be pushed to the third row of 'out' matrix. This process has to be operated with respect to m,p and seq.

採用された回答

Roger Stafford
Roger Stafford 2013 年 12 月 30 日
m = 3;
p = [ 5, 8,10,11, 6, 7, 9];
seq = [ 3, 4, 2, 6, 5, 1, 7];
N = size(seq,2);
n = ceil(N/m);
out = reshape([p(seq),zeros(1,n*m-N)],m,n)';
c = zeros(1,m);
for k = 1:n
[~,q] = sort(c);
out(k,q) = out(k,:);
c = c + out(k,:);
end
  1 件のコメント
Bathrinath
Bathrinath 2013 年 12 月 31 日
Thank you very much sir,I have written the code for nearly 65 lines for this program and also didn't work but you have written it in 12 lines that is working fine. you are genius!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by