Filter löschen
Filter löschen

Argument on 2 vectors without using for loop

2 Ansichten (letzte 30 Tage)
Sourangsu Chowdhury
Sourangsu Chowdhury am 29 Okt. 2018
Beantwortet: madhan ravi am 29 Okt. 2018
I have 2 vectors
A=[10 22 31 14 55 16 47 18];
B= [1 2 1 1 1 2 1 2];
I want to make a third vector C which contains values of A for values of B=1; like
C=[10 NaN 31 14 55 NaN 47 NaN]
or
C=[10 31 14 55 47]
I did this using a for loop. Just want to know if this is doable without using for loop as it may save a lot of computation time. (The vectors with me are really really large
for i=1:length (A);
if B(i)==1;
C(i)=A(i)
else C(i)=NaN;
end;
end;

Akzeptierte Antwort

madhan ravi
madhan ravi am 29 Okt. 2018
A=[10 22 31 14 55 16 47 18];
B= [1 2 1 1 1 2 1 2];
C=A(B==1)

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by