how can I reshape vector data into a matrix

조회 수: 6 (최근 30일)
Daria Bontch-Osmolovskaia
Daria Bontch-Osmolovskaia 2018년 7월 31일
Can anyone help me with this please... I have a data set, shaped so. This needs to be graphed in 3D.
The first column represents locations on X-axis, the 2nd column - locations on Y-axis, and the third column is my data. The 1st & 2nd column make a grid.
How do I reshape this to make the 3rd column into a 2x2 matrix, so that the whole thing can be graphed as a surface?
Thank you beforehand!
D.

채택된 답변

Guillaume
Guillaume 2018년 7월 31일
How do I reshape this to make the 3rd column into a 2x2 matrix, so that the whole thing can be graphed as a surface?
You don't, it's completely unnecessary
surf(yourmatrix(:, 1), yourmatrix(:, 2), yourmatrix(:, 3));
If you wanted to do the reshape (which again is completely unnecessary), and assuming that the X and Y coordinates that are supposed to be identical are actually identical down to the last binary digit, then:
[~, ~, col] = unique(yourmatrix(:, 1));
[~, ~, row] = unique(yourmatrix(:, 2));
X(row, col) = yourmatrix(:, 1);
Y(row, col) = yourmatrix(:, 2);
Z(row, col) = yourmatrix(:, 3);
  댓글 수: 4
Guillaume
Guillaume 2018년 7월 31일
Doh! Indeed Z has to be a matrix. And the second part of the answer was also incorrect. Never mind, this is correct:
[X, ~, col] = unique(yourmatrix(:, 1));
[Y, ~, row] = unique(yourmatrix(:, 2));
Z = zeros(numel(Y), numel(X));
Z(sub2ind(size(Z), row, col)) = yourmatrix(:, 3));
Daria Bontch-Osmolovskaia
Daria Bontch-Osmolovskaia 2018년 8월 1일
Guillame, thank you very much - that worked beautifully. Quite literally. Looks like the aurora :) Really appreciate your effort. Prior to this, I was only able to graph data interpolated, and that didn't work right. This is much better as I can see the crazy outliers. Cheers, Daria

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Discrete Data Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by