Remove NaN values from array and corresponding indices in another array
9 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Megan Henriksen
am 26 Nov. 2018
Bearbeitet: madhan ravi
am 26 Nov. 2018
Hi there:
Let's say I have an array a = [1,2,NaN,3] and another array b= [4,5,6,7]. I want to remove the NaN from a, and then the value of the corresponding index from b (which in this case, would be the value 6).
Right now, this is what I'm trying to do:
a1 = isfinite(a);
b1 = [ ];
for value = b
if a1(value) == 1
b1 = [b1, b(value)];
end
end
However, this assumes that for value = b is assigning value as the index of the numbers in d, not the values themselves, which is incorrect. I want to try to redo that statement or the contents of the loop in order to go through all of values in b, but get their corresponding indices to use to index into a1 and b when I'm inside the loop.
Any suggestions on how to do this? Or a more efficient method? I've also tried setting up a filter, but I was getting confused.
0 Kommentare
Akzeptierte Antwort
madhan ravi
am 26 Nov. 2018
Bearbeitet: madhan ravi
am 26 Nov. 2018
no loops needed:
>> a = [1,2,NaN,3];
b = [4,5,6,7];
b(isnan(a))=[]
a(isnan(a))=[]
b =
4 5 7
a =
1 2 3
>>
or
>>c = [a(:) b(:)] %put them in a matrix and remove the row containing nan
c(isnan(c),:)=[]
c =
1 4
2 5
NaN 6
3 7
c =
1 4
2 5
3 7
>>
2 Kommentare
Weitere Antworten (0)
Siehe auch
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!