how to use unique in a loop
3 次查看(过去 30 天)
显示 更早的评论
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
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.
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!