Fetching data to the matrix in a particular sequence

1 visualización (últimos 30 días)
Bathrinath
Bathrinath el 30 de Dic. de 2013
Comentada: Bathrinath el 31 de Dic. de 2013
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.

Respuesta aceptada

Roger Stafford
Roger Stafford el 30 de Dic. de 2013
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 comentario
Bathrinath
Bathrinath el 31 de Dic. de 2013
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!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by