# Matrix Filling

1 view (last 30 days)
Ahsan Khan on 6 Jun 2012
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
Sean de Wolski on 6 Jun 2012
And what do you want to do if the random integer is 59?

Wayne King on 6 Jun 2012
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 CommentsShowHide 1 older comment
Ahsan Khan on 6 Jun 2012
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