Find out the cell index from matric values

2 Ansichten (letzte 30 Tage)
Lama Hamadeh
Lama Hamadeh am 31 Mär. 2022
Kommentiert: Akira Agata am 1 Apr. 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.

Akzeptierte Antwort

Akira Agata
Akira Agata am 31 Mär. 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 Kommentare
Lama Hamadeh
Lama Hamadeh am 31 Mär. 2022
Thanks. Would that work if x and y are split differently? Instead of having an grid, we have ?
Akira Agata
Akira Agata am 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);

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by