how to use unique in a loop
3 views (last 30 days)
Show older comments
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
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.
Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!