Effacer les filtres
Effacer les filtres

Find out the cell index from matric values

1 vue (au cours des 30 derniers jours)
Lama Hamadeh
Lama Hamadeh le 31 Mar 2022
Commenté : Akira Agata le 1 Avr 2022
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.

Réponse acceptée

Akira Agata
Akira Agata le 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 commentaires
Lama Hamadeh
Lama Hamadeh le 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 le 1 Avr 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);

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by