Find out the cell index from matric values

조회 수: 1 (최근 30일)
Lama Hamadeh
Lama Hamadeh 2022년 3월 31일
댓글: Akira Agata 2022년 4월 1일
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.

채택된 답변

Akira Agata
Akira Agata 2022년 3월 31일
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
Lama Hamadeh
Lama Hamadeh 2022년 3월 31일
Thanks. Would that work if x and y are split differently? Instead of having an grid, we have ?
Akira Agata
Akira Agata 2022년 4월 1일
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);

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by