problem with if in a matrix
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Tristan
le 29 Sep 2013
Modifié(e) : Azzi Abdelmalek
le 29 Sep 2013
I have a matrix g:
>> g=sin((1:1:10)'*(1:1:2))
g =
0.8415 0.9093
0.9093 -0.7568
0.1411 -0.2794
-0.7568 0.9894
-0.9589 -0.5440
-0.2794 -0.5366
0.6570 0.9906
0.9894 -0.2879
0.4121 -0.7510
-0.5440 0.9129
when I add the two columns together I get
(g(:, [1])+g(:, [2]))
ans =
1.7508
0.1525
-0.1383
0.2326
-1.5029
-0.8160
1.6476
0.7015
-0.3389
0.3689
I want matlab to return the results of the first column when the results are positive and of the second column when the results are negative, so I tried:
if(g(:, [1])+g(:, [2]))>0
g(:, [1])
else
g(:, [2])
end
but that just gives me the results of the second column.
0 commentaires
Réponse acceptée
Azzi Abdelmalek
le 29 Sep 2013
Modifié(e) : Azzi Abdelmalek
le 29 Sep 2013
idxn=sum(g,2)<0
out=g(:,1);
out(idxn)=g(idxn,2)
0 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Get Started with MATLAB dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!