Matrix Filling

2 visualizaciones (últimos 30 días)
Ahsan Khan
Ahsan Khan el 6 de Jun. de 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 comentario
Sean de Wolski
Sean de Wolski el 6 de Jun. de 2012
And what do you want to do if the random integer is 59?

Iniciar sesión para comentar.

Respuesta aceptada

Wayne King
Wayne King el 6 de Jun. de 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 comentarios
Wayne King
Wayne King el 6 de Jun. de 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 el 6 de Jun. de 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

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Matrices and Arrays en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by