how to use unique in a loop

3 次查看(过去 30 天)
Dean Ranmar
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