array fetching and placing in matrix

1 visualización (últimos 30 días)
Bathrinath
Bathrinath el 6 de Sept. de 2016
Editada: Stephen23 el 6 de Sept. de 2016
I have a matrix c = [0 0 0; 0 0 0; 0 0 0] from the command c=zeros(3,3). My array p=[12,10,11,13,8,9,5]; I need to get array in c and my final value of c should be [12,10,11;9,13,8;0,0,5]. First 3 array value should be placed in first row of c, in the second row of c, it has to look the minimum value in the first row (min value =10) corresponding column it has to place the fourth value in array. Similarly fifth and sixth value in array p is placed. now in the third row of matrix c, first two rows of c has to be added to get the minimum value. after the summation minimum value is in third column so the seventh value in the array p is placed in third column of matrix c. This procedure has to be done irrespective of the array size and matrix. kindly suggest.
  2 comentarios
Guillaume
Guillaume el 6 de Sept. de 2016
I don't understand your algorithm. Why, when the minimum of the first row is 10, do you select the 6th value (equal to 9) of p to place in C(2, 1)?
Bathrinath
Bathrinath el 6 de Sept. de 2016
dear sir, i am giving the array in that sequence that is having the index value 1,2,3,4,5,6,7. so first row will be filled by the index 1,2,3 and in the second row fourth index has to be placed in minimum value of the first row index column

Iniciar sesión para comentar.

Respuesta aceptada

Stephen23
Stephen23 el 6 de Sept. de 2016
Editada: Stephen23 el 6 de Sept. de 2016
Edit works for input matrices of any size:
% input data:
p = [12,10,11,13,8,9,5];
c = zeros(3,3);
% input sizes:
[row,col] = size(c);
len = numel(p);
% allocate data:
c = c.';
idx = 1:min(len,row*col);
c(idx) = p(idx);
c = c.';
% sort rows:
for k = 2:row
[~,idx] = sort(c(k-1,:));
c(k,idx) = c(k,:);
end
creates this:
>> c
c =
12 10 11
9 13 8
0 0 5
  5 comentarios
Stephen23
Stephen23 el 6 de Sept. de 2016
Editada: Stephen23 el 6 de Sept. de 2016
@Bathrinath: like any skill, learning how to create and implement algorithms takes time and practice. You can start learning how to use MATLAB by doing these tutorials:
and read this carefully:
The best thing you can do is to practice and learn to read the documentation: MATLAB has excellent documentation, with lots of working examples and articles explaining many topics. Learn how to navigate it, browse the contents hierarchy, and use the links effectively.
Bathrinath
Bathrinath el 6 de Sept. de 2016
thank you sir for your valuable input.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Matrix Indexing 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