필터 지우기
필터 지우기

How to compare two arrays?

조회 수: 2 (최근 30일)
Micky
Micky 2013년 2월 28일
Example:
aa = [1 1 1 1 -1 -1 1 1 1 1 1 1 -1 -1 1]
and
bb = [NaN NaN NaN NaN 0.2 0.3 NaN 0.2 0.3 NaN NaN NaN 0.2 0.3 NaN]
and I want to generate third array such that it contains values in bb for every value that correspond to -1 in aa, and for every other value I should get NaN, and the size of resulted should remain same as aa and bb. So the result should look something like this:
cc = [NaN NaN NaN NaN 0.2 0.3 NaN NaN NaN NaN NaN NaN 0.2 0.3 NaN].
I am doing something like this:
cc = bb(aa == -1 | isnan(bb));
But the length or the size of the array changes.
Please advice.

채택된 답변

Image Analyst
Image Analyst 2013년 2월 28일
Try this:
cc = nan(1, length(bb))
cc(aa==-1) = bb(aa==-1)
In the command window:
cc =
NaN NaN NaN NaN 0.2000 0.3000 NaN NaN NaN NaN NaN NaN 0.2000 0.3000 NaN
  댓글 수: 1
Micky
Micky 2013년 2월 28일
Thank you Image analyst. Both of the responses are almost similar.

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

추가 답변 (1개)

Azzi Abdelmalek
Azzi Abdelmalek 2013년 2월 28일
편집: Azzi Abdelmalek 2013년 2월 28일
cc=nan(1,numel(aa))
idx=find(aa==-1)
cc(idx)=bb(idx)
  댓글 수: 1
Micky
Micky 2013년 2월 28일
Thank You Azzi.

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by