Converting a table to a matrix based on coordinates

10 ビュー (過去 30 日間)
Natalia
Natalia 2023 年 6 月 2 日
コメント済み: chicken vector 2023 年 6 月 2 日
Hi,
I have a table which contains 3 variables: X coordinate (X), Y coordinate (Y) and a value in point (Int). X and Y coordinates are equally spaced. I want to convert it to a matrix, in which the position of an Int value in the matrix will represent its coordinates. How to do this?
Thanks in advance

採用された回答

chicken vector
chicken vector 2023 年 6 月 2 日
編集済み: chicken vector 2023 年 6 月 2 日
Be careful because Matlab uses inverted indeces so this way you have X as rows and Y as columns.
Just invert the indeces in the loop to invert this behaviour.
% Setup table for example:
x = 0:.1:.5;
y = 0:.2:1;
z = 0:5;
T = table(x', y', z', 'VariableNames', {'X', 'Y', 'Value'})
T = 6×3 table
X Y Value ___ ___ _____ 0 0 0 0.1 0.2 1 0.2 0.4 2 0.3 0.6 3 0.4 0.8 4 0.5 1 5
Now we extract some information about X and Y coordinates.
If your X and Y are in the form:
1:10
ans = 1×10
1 2 3 4 5 6 7 8 9 10
Then this part is not required.
% Initialise conversion:
nData = size(T,1);
matrixData = zeros(nData);
xStep = diff(T{[1,2],1});
yStep = diff(T{[1,2],2});
xOffset = xStep - T{1,1};
yOffset = yStep - T{1,2};
Finally we loop over the each row of the table to move the values in the matrix:
% Allocate table's values:
for j = 1 : nData
xMatrix = int64((T{j,1} + xOffset) / xStep);
yMatrix = int64((T{j,2} + yOffset) / yStep);
matrixData(xMatrix, yMatrix) = T{j, 3};
end
This is the result:
% Display result:
matrixData
matrixData = 6×6
0 0 0 0 0 0 0 1 0 0 0 0 0 0 2 0 0 0 0 0 0 3 0 0 0 0 0 0 4 0 0 0 0 0 0 5
  6 件のコメント
Natalia
Natalia 2023 年 6 月 2 日
Thanks a lot! This looks exactly like what I need :)
chicken vector
chicken vector 2023 年 6 月 2 日
Glad to help

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeArgument Definitions についてさらに検索

タグ

製品


リリース

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by