Clear Filters
Clear Filters

how to use unique in a loop

4 views (last 30 days)
Dean Ranmar
Dean Ranmar on 12 May 2019
Commented: Dean Ranmar on 12 May 2019
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 Comments
Rik
Rik on 12 May 2019
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 on 12 May 2019
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.

Sign in to comment.

Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products


Release

R2015b

Community Treasure Hunt

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

Start Hunting!