Matrix Filling

1 Ansicht (letzte 30 Tage)
Ahsan Khan
Ahsan Khan am 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
how can i do that. please help
  1 Kommentar
Sean de Wolski
Sean de Wolski am 6 Jun. 2012
And what do you want to do if the random integer is 59?

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Wayne King
Wayne King am 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 Kommentare
Wayne King
Wayne King am 6 Jun. 2012
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 am 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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by