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!