Determine indices of how many times (x,y) coordinates occur in 2d vector

7 ビュー (過去 30 日間)
Torbjörn Olsson
Torbjörn Olsson 2020 年 11 月 4 日
回答済み: Star Strider 2020 年 11 月 4 日
I wand to check if 2d coordinates of type (x,y) occur many times in 2d vector. I just want to count the number of times they occur if they occur at least two times and I also need the indices for this secondt (and beyond) times they occur.

採用された回答

Star Strider
Star Strider 2020 年 11 月 4 日
Try this:
M = randi(5, 25, 2); % Create ‘LatLon’ Data
[Mu,~,idx] = unique(M, 'rows'); % Unique Rows
Tally = accumarray(idx, (1:numel(idx)).', [], @(x) {M(x,:)}); % Calculate Frequencies, Return Pairs By Group
pairs = cellfun(@(x)size(x,1), Tally); % Recover ‘LatLon’ Pairs
idxc = accumarray(idx, (1:numel(idx)).', [], @(x) {x.'}); % Get Indices
idxmtx = zeros(numel(pairs),max(pairs(:))); % Create Matrix For Indices
for k = 1:size(idxmtx,1)
idxmtx(k,1:pairs(k)) = [idxc{k}]; % Assign Indices To Appropriate Rows
end
[~,ixs] = sort(pairs, 'descend'); % Sort Them
Result = table(Mu(ixs,:),pairs(ixs),idxmtx(ixs,:), 'VariableNames',{'LatLon','Frequency','Indices'}); % Output Table Of Pairs & Frequencies
ResultSel = Result(pairs(ixs) > 1,:) % Select Only Occurrences > 1
Substitute your coordinates for the ‘M’ matrix in my code.
If the coordinates are not exact, consider substituting uniquetol with the ByRows name-value pair for unique.
.

その他の回答 (1 件)

Ramya Dodla
Ramya Dodla 2020 年 11 月 4 日
編集済み: Ramya Dodla 2020 年 11 月 4 日
MATLAB ismember function will provide you with the indices of (x,y) occurance. Try the following code. This will give you a 5x1 array where 1 represent (x,y) occurance.
x = 0;
y = 1;
arr=[2 0 0 0 0; 1 1 1 1 1];
presentMatrix = ismember(arr.', [x y], 'rows');
The following code should provide number of occurances
sum(presentMatrix(:) == 1)

カテゴリ

Help Center および File ExchangeShifting and Sorting Matrices についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by