Converting a table to a matrix based on coordinates
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
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
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
2023 年 6 月 2 日
Thanks for a quick answer :) I'm afraid your solution doesn't take into account that in the table there are multiple rows with the same X or Y, only X and Y combinations are unique - sorry, I should've specified it from the beginning. As an effect I get a 1050x1050 matrix, not 35x30. Is there any way to assign both X and Y to an Int value?
chicken vector
2023 年 6 月 2 日
編集済み: chicken vector
2023 年 6 月 2 日
I can make this code more general than it is now and it will probably work for you, but if you tell me the initial values of X and Y and their step it will be much easier.
Meanwhile you can try this:
% Setup fake table for example:
xData = 0:.1:.3;
yData = -.2:.2:.2;
nX = length(xData);
nY = length(yData);
x = repmat(xData, nY, 1);
y = repmat(yData', 1, nX);
z = randi(10, nX, nY);
T = table(x(:), y(:), z(:), 'VariableNames', {'X', 'Y', 'Value'})
T = 12×3 table
X Y Value
___ ____ _____
0 -0.2 4
0 0 7
0 0.2 8
0.1 -0.2 8
0.1 0 4
0.1 0.2 9
0.2 -0.2 10
0.2 0 7
0.2 0.2 2
0.3 -0.2 4
0.3 0 5
0.3 0.2 9
% Extract data from table:
xCoord = unique(T{:,1});
yCoord = unique(T{:,2});
% Initialise conversion:
matrixData = zeros(length(xCoord), length(yCoord));
xStep = diff(xCoord([1,2]));
yStep = diff(yCoord([1,2]));
xOffset = xStep - xCoord(1);
yOffset = yStep - yCoord(1);
% Allocate table's values:
for j = 1 : length(T{:,1})
xMatrix = int64((T{j,1} + xOffset) / xStep);
yMatrix = int64((T{j,2} + yOffset) / yStep);
matrixData(xMatrix, yMatrix) = T{j, 3};
end
matrixData
matrixData = 4×3
4 7 8
8 4 9
10 7 2
4 5 9
bar3(matrixData, 'r');

Natalia
2023 年 6 月 2 日
Here is the table (unfortunately needs some adjustments: Delimiter=" ", DecimalSeparator="," and stuff). The step is 1.5 for both X and Y.
chicken vector
2023 年 6 月 2 日
編集済み: chicken vector
2023 年 6 月 2 日
This works for me:
% Load table:
filename = 'NG-204-205 5% wytl mapa2_fit2 IntG.txt';
T = readtable(filename, 'Decimal', ',');
% Extract data from table:
xCoord = unique(T{:,1});
yCoord = unique(T{:,2});
% Initialise conversion:
matrixData = zeros(length(xCoord), length(yCoord));
xStep = diff(xCoord([1,2]));
yStep = diff(yCoord([1,2]));
xOffset = xStep - xCoord(1);
yOffset = yStep - yCoord(1);
% Allocate table's values:
for j = 1 : length(T{:,1})
xMatrix = int64((T{j,1} + xOffset) / xStep);
yMatrix = int64((T{j,2} + yOffset) / yStep);
matrixData(xMatrix, yMatrix) = T{j, 3};
end

Natalia
2023 年 6 月 2 日
Thanks a lot! This looks exactly like what I need :)
chicken vector
2023 年 6 月 2 日
Glad to help
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Spreadsheets についてさらに検索
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
