Remove NaN values from array and corresponding indices in another array
9 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
madhan ravi
2018-11-26
编辑:madhan ravi
2018-11-26
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 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!