필터 지우기
필터 지우기

Compare two vector arrays and if they match set vector array 2 = nan?

조회 수: 1 (최근 30일)
MKM
MKM 2022년 2월 11일
댓글: Arif Hoq 2022년 2월 11일
So i have two arrays the same size:
time = [0,0,0,0,2,3,4,5,5,6,7,8]';
solution = [1,2,0,0,1,1,2,2,2,0,0,0];
What would be the best way to compare the two and if both are the same make solution 0 = nan;
So the final outcome i would like is solution = [1,2,nan,nan,1,1,2,2,2,0,0,0];
would i use a function such as:
if strfind(time,solution)==0
Do something?
Thanks in adavnce!

채택된 답변

Walter Roberson
Walter Roberson 2022년 2월 11일
time = [0,0,0,0,2,3,4,5,5,6,7,8]
time = 1×12
0 0 0 0 2 3 4 5 5 6 7 8
solution = [1,2,0,0,1,1,2,2,2,0,0,0]
solution = 1×12
1 2 0 0 1 1 2 2 2 0 0 0
outcome = solution;
outcome(solution == 0 & time == 0) = nan
outcome = 1×12
1 2 NaN NaN 1 1 2 2 2 0 0 0
It is not clear what you want to have happen in positions where the values are equal, but the values are not 0.
  댓글 수: 3
MKM
MKM 2022년 2월 11일
if any(time==0 & solution == 0)
solution(time ==0 & solution==0)=nan;
else
return
end
Used this solution in the end, so the solution is - update where time and solution 0s are equal and if not return to script
Walter Roberson
Walter Roberson 2022년 2월 11일
solution(time ==0 & solution==0) = nan;
if ~any(isnan(solution)); return; end

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

추가 답변 (1개)

Arif Hoq
Arif Hoq 2022년 2월 11일
편집: Arif Hoq 2022년 2월 11일
Try this...
time = [0,0,0,0,2,3,4,5,5,6,7,8];
solution = [1,2,0,0,1,1,2,2,2,0,0,0];
[idx]=find(time==solution);
solution(idx)=[NaN NaN]
solution = 1×12
1 2 NaN NaN 1 1 2 2 2 0 0 0
  댓글 수: 2
Stephen23
Stephen23 2022년 2월 11일
FIND is not required, logical indexing is simpler and more efficient.
It would be better to allocate a scalar NaN.
Arif Hoq
Arif Hoq 2022년 2월 11일
@Stephen Thank you very much for the clue

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

카테고리

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

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by