필터 지우기
필터 지우기

How can I compare two rows of an array withot for loop?

조회 수: 2 (최근 30일)
Collegue
Collegue 2020년 4월 28일
댓글: Ameer Hamza 2020년 4월 28일
Hi! I want to compare two rows of an array. I want to do this:
I hace a = [1 NaN 2 NaN;[2 NaN NaN 3];
I want to create another array without using for loop iteration and obtain the next array
--> If both have a value I want to obtain the mean value of them.
--> if one of them have NaN I want to obtain the number
--> If both have NaN I want to delete it
I want to obtain this:
c = [1,NaN,2,3]

채택된 답변

Ameer Hamza
Ameer Hamza 2020년 4월 28일
Try this
a = [1 NaN 2 NaN];
b = [2 NaN NaN 3];
c = nanmean([a; b])
Result:
c =
1.5000 NaN 2.0000 3.0000
  댓글 수: 4
Collegue
Collegue 2020년 4월 28일
YEs but know I want to sompare them. If both of them have a value I want to substract and if one of them have NaN obtain the other value
Ameer Hamza
Ameer Hamza 2020년 4월 28일
Following code is for a-b, If both are NaN, the output is also NaN.
a = [1 NaN 2 NaN];
b = [2 NaN NaN 3];
x = [a;b];
mask = all(isnan(x));
c = nansum([a; -b]);
c(mask) = nan;
Result
>> c
c =
-1 NaN 2 -3

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Cell Arrays에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by