array fetching and placing in matrix

1 次查看(过去 30 天)
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 个评论
Guillaume
Guillaume 2016-9-6
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 2016-9-6
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

请先登录,再进行评论。

采纳的回答

Stephen23
Stephen23 2016-9-6
编辑:Stephen23 2016-9-6
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 个评论
Stephen23
Stephen23 2016-9-6
编辑:Stephen23 2016-9-6
@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 2016-9-6
thank you sir for your valuable input.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by