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!