How can I convert a cell to a matrix ?

조회 수: 2 (최근 30일)
Anthony Cazin-Bourguignon
Anthony Cazin-Bourguignon 2020년 11월 4일
댓글: Ameer Hamza 2020년 11월 4일
Hello, I have a table that I would like to change into a matrix as below.
I tried "cell2matrix" but it doesn't work and I really don't know how to do it. It's for a job and I am very grateful to the person who will help me.
Thank you for your time
Anthony

채택된 답변

Stephen23
Stephen23 2020년 11월 4일
편집: Stephen23 2020년 11월 4일
The simple and efficient approach:
X = reshape([output{:,1}],260,207);
y = reshape([output{:,2}],260,207);
Z = reshape([output{:,3}],260,207);

추가 답변 (3개)

Ameer Hamza
Ameer Hamza 2020년 11월 4일
편집: Ameer Hamza 2020년 11월 4일
If first matrix contains all combinations of row and columns in the order you mentioned,
C = [
1 1 1;
2 1 2;
3 1 3;
1 2 4;
2 2 5;
3 2 6;
1 3 7;
2 3 8;
3 3 9];
M = reshape(C(:,3), 3, 3)
For a general case, in which indexes in column 1 and 2 have an arbitrary order.
M = zeros(max(C(:,1)), max(C(:,2)));
idx = sub2ind(size(M), C(:,1), C(:,2));
M(idx) = C(:,3);
  댓글 수: 7
Anthony Cazin-Bourguignon
Anthony Cazin-Bourguignon 2020년 11월 4일
Here u go
Ameer Hamza
Ameer Hamza 2020년 11월 4일
Ok, I get it now. scatteredInterpolant seems suitable here:
x = unique([output{:,1}]);
y = unique([output{:,2}]);
[Xg, Yg] = ndgrid(x, y);
mdl = scatteredInterpolant([output{:,1}].', [output{:,2}].', [output{:,3}].');
M = mdl(Xg, Yg);
To see the relation between value of x, y and data column, run the following code
T = array2table(M, 'VariableNames', string(y));
T.Row = string(x);

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


Rik
Rik 2020년 11월 4일
You need to use accumarray to create this. You can find examples in the documentation for that function:
doc accumarray
  댓글 수: 2
Anthony Cazin-Bourguignon
Anthony Cazin-Bourguignon 2020년 11월 4일
Hello Rik, I've read the documentation but I don't really see how this could solve my problem. Would it be possible for you to show me a small example with the values of the photo I put in the question? I'm still a beginner in matlab
Thank you
Rik
Rik 2020년 11월 4일
Your example only contained integer indices, while the picture of your actual data contains decimal values. You need to make sure you have a set of integer values, which you could do with unique:
x=[-3.53;-3.53;-4.53;-4.53];
y=[51.05;51.10;51.05;51.10];
z=rand(size(x));
[x_unique,~,x_int]=unique(x);
[y_unique,~,y_int]=unique(y);
subs=[x_int y_int];
data=accumarray(subs,z);

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


Steven Lord
Steven Lord 2020년 11월 4일
What specific type of variable are you using to store your data? If it's in a table array take a look at the unstack function. The first example on that documentation page looks very similar to your picture.
  댓글 수: 1
Anthony Cazin-Bourguignon
Anthony Cazin-Bourguignon 2020년 11월 4일
Hi, my data are stored in a cell of 53820x3 and I need to make a matrix of 260x207 with it. I dont know if it's work with unstack function

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

카테고리

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