필터 지우기
필터 지우기

how to use unique in a loop

조회 수: 4 (최근 30일)
Dean Ranmar
Dean Ranmar 2019년 5월 12일
댓글: Dean Ranmar 2019년 5월 12일
I'm trying to use unique as a for loop control variable and getting an error.
In code I used about two years ago, I used this format:
___________________________________________________________
for nTrDop = unique(trDp); % cycle unique detection Doppler locations
indRng = (trDp == nTrDop); % all range indices for given Doppler
rngPerDop = trRg(indRng); % range values for range indices
tmpTrPwr = trPCRDMap(rngPerDop, nTrDop); % amps for range locations
............ more processing on set of range-Doppler signal amplitudes .........
end
(I've abbreviated the code for two reasons: 1) some is proprietary; 2) it's
irrelevant to my question.)
__________________________________________________________
Today, I tried to use what seems to be the same format but get an error with
this code:
___________________________________________________________
for nCPI = unique(E1);
id1 = (E1 == nCPI);
F1 = E1(id1); % get MofN1 rows for CPI #nCPI
.............. again, more processing
___________________________________________________________
The error occurs on ' id1 = (E1 == nCPI)' : "Matrix dimensions must agree"
and I see that, generally, E1 and nCPI ARE different sizes. However, in the
earlier code, it seems I was able to compare individual elements of 'trDp' to
individual values 'nTrDop' using this format. Has MATLAB code changed?
If not, can someone identify the problem?
  댓글 수: 2
Rik
Rik 2019년 5월 12일
This code will only work as expected when it is a vector, and then maybe only either a row or a column vector (I'm on mobile so I can't check).
Why don't you use ismember? It can both provide the unique values, and the locations. Or even the second output of unique, which could also be helpful here.
Dean Ranmar
Dean Ranmar 2019년 5월 12일
Thanks, Rik. The variables in questions are vectors. I will see what I can do with ismember. I did write a workaround but I don't yet understand why the new code fails. Even if I find a better or just-as-good solution with ismember, I've found it always pays to understand WHY something doesn't work ... Again, thanks.

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

답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품


릴리스

R2015b

Community Treasure Hunt

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

Start Hunting!

Translated by