How can i alternating signs in every line in a loop

11 vues (au cours des 30 derniers jours)
Sebastian Gondzik
Sebastian Gondzik le 18 Fév 2021
Commenté : Adam Danz le 22 Fév 2021
alpha6=zeros(1,6)
for i = 1:90
alpha6(:,:,i)=i;
alpha6(:,2:2:end)= (alpha6(:,2:2:end))*-1
end
  1 commentaire
Adam Danz
Adam Danz le 22 Fév 2021
By "every line" do you mean every column, every row, or something else?

Connectez-vous pour commenter.

Réponses (1)

Adam Danz
Adam Danz le 18 Fév 2021
Modifié(e) : Adam Danz le 19 Fév 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));

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by