Matrix Filling

1 次查看(过去 30 天)
Ahsan Khan
Ahsan Khan 2012-6-6
hi MATLABERS,
Im stuck at this problem. I want to make a matrix that does the following:
say i have the first row of the matrix [12 14 18]. now a get a random integer and it comes out to be 14. I want to store that 14 right under the 14 like so:
[12 14 18;0 14 0]
12 14 18
0 14 0
or
12 14 18
14
how can i do that. please help
  1 个评论
Sean de Wolski
Sean de Wolski 2012-6-6
And what do you want to do if the random integer is 59?

请先登录,再进行评论。

采纳的回答

Wayne King
Wayne King 2012-6-6
index = find(A(1,:) == 14);
A = cat(1,A,zeros(1,size(A,2)));
A(2,index) = 14;
Added after Sean's point -- if the integer is not present in the row, you'll just get a row of zeros.
index = find(A(1,:) == 59);
A = cat(1,A,zeros(1,size(A,2)));
A(2,index) = 59;
  2 个评论
Wayne King
Wayne King 2012-6-6
Sean has a very good point, note that in that case, the above will just fill with a row of zeros.
Ahsan Khan
Ahsan Khan 2012-6-6
thanks this works but how do i do this with a array of integers instead of just one.
ex predefined [1 2 3 4 5 6 7 8 9 10] is compared with a matrix [2 5 7;1 2 9 10;4 5 6] and the results should be as follows:
1 2 3 4 5 6 7 8 9 10
0 2 0 0 5 0 7 0 0 0
1 2 0 0 0 0 0 0 9
0 0 0 4 5 6 0 0 0

请先登录,再进行评论。

更多回答(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