How can i alternating signs in every line in a loop
11 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
alpha6=zeros(1,6)
for i = 1:90
alpha6(:,:,i)=i;
alpha6(:,2:2:end)= (alpha6(:,2:2:end))*-1
end
1 commentaire
Réponses (1)
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));
0 commentaires
Voir également
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!