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 CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by