Matrix Filling

3 visualizzazioni (ultimi 30 giorni)
Ahsan Khan
Ahsan Khan il 6 Giu 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 Commento
Sean de Wolski
Sean de Wolski il 6 Giu 2012
And what do you want to do if the random integer is 59?

Accedi per commentare.

Risposta accettata

Wayne King
Wayne King il 6 Giu 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 Commenti
Wayne King
Wayne King il 6 Giu 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 il 6 Giu 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

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Matrices and Arrays in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by