How can i alternating signs in every line in a loop
11 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
alpha6=zeros(1,6)
for i = 1:90
alpha6(:,:,i)=i;
alpha6(:,2:2:end)= (alpha6(:,2:2:end))*-1
end
1 comentario
Adam Danz
el 22 de Feb. de 2021
By "every line" do you mean every column, every row, or something else?
Respuestas (1)
Adam Danz
el 18 de Feb. de 2021
Editada: Adam Danz
el 19 de Feb. de 2021
To flip the sign of every second column of a matrix, starting with column 2,
M(:,2:2:end) = -1*M(:,2:2:end);
Note that this may not alternate signs if some values are negative to begin with. If you want to ensure that every second column starting with col 2 is negative and the other columns are positive,
M(:,2:2:end) = -1*abs(M(:,2:2:end));
0 comentarios
Ver también
Categorías
Más información sobre Matrix Indexing 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!