how to change values in matrix by indices
Mostrar comentarios más antiguos
Hi, i need to change all the values in the index where i,j are both even or odd to zero and multiple by 2 all the other. for example:
change
1,2,3
4,5,6
7,8,9
to
0,4,0
8,0,12
0,16,0
1 comentario
James Tursa
el 28 de Abr. de 2015
Editada: James Tursa
el 28 de Abr. de 2015
Sounds like homework. Are you instructed to use any particular method (for loop, logical indexing, etc)?
Respuestas (4)
James Tursa
el 28 de Abr. de 2015
2 votos
Andrei Bobrov
el 30 de Abr. de 2015
variant:
a = [1,2,3
4,5,6
7,8,9];
[i1,i2] = ndgrid(1:size(a,1));
out = rem(i1+i2,2)*2.*a;
Thorsten
el 30 de Abr. de 2015
index = 1:9;
ieven = mod(index, 2) ==0;
index(ieven) = 2*index
index(~ieven) = 0
Categorías
Más información sobre Matrix Indexing en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!