Remove NaN values from array and corresponding indices in another array

9 Ansichten (letzte 30 Tage)
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.

Akzeptierte Antwort

madhan ravi
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
>>

Weitere Antworten (0)

Kategorien

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

Produkte


Version

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by