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 Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by