Find out the cell index from matric values

1 visualizzazione (ultimi 30 giorni)
Hi all,
I want to find out the cell index of each row of matrix A. Each row has two values that correspond to x and y. I need to construct an index matrix that has the cell indeicies of each row depening on the x and y values. The below example hopefully clarifies the idea:
Here is my code attempt:
for i = 1:size(A,1)
for n = 1:numel(x)
for m = 1:numel(y)
%check the limits of each cell
if (A(i,1) <= x(n)+dx || A(i,1) >= x(n)) && ...
(A(i,2) <= y(m)+dy || A(i,2) >= y(m))
%find out the cell index
%Here where I need help!
end
end
end
end
Thanks.

Risposta accettata

Akira Agata
Akira Agata il 31 Mar 2022
How about using histcount2 ?
The following is an example:
% Sample data
A = [...
0.7, 0.1;...
0.1, 0.2;...
0.8, 0.6;...
0.3, 0.7];
% Edge for histcount2
edge = [0, 0.5, 1];
% Find x-bin and y-bin IDs for each data point
[~, ~, ~, binx, biny] =...
histcounts2(A(:,1), A(:,2), edge, edge);
% Convert x-bin and y-bin IDs to cell ID
cellID = biny + 2*(binx-1);
% Display the result
disp(cellID)
3 1 4 2
  2 Commenti
Lama Hamadeh
Lama Hamadeh il 31 Mar 2022
Thanks. Would that work if x and y are split differently? Instead of having an grid, we have ?
Akira Agata
Akira Agata il 1 Apr 2022
Yes, of course! You can do that task by setting different edges for x- and y-axes, like:
% Edge for histcount2
edge_x = 0:0.5:1;
edge_y = 0:0.2:1;
% Find x-bin and y-bin IDs for each data point
[~, ~, ~, binx, biny] =...
histcounts2(A(:,1), A(:,2), edge_x, edge_y);

Accedi per commentare.

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by