How can I convert 2 column matrix to a cell array?

1 回表示 (過去 30 日間)
Jay Vaidya
Jay Vaidya 2019 年 12 月 30 日
編集済み: Jay Vaidya 2020 年 1 月 1 日
Is there a convenient way to convert a 2 column matrix into a cell array (without using nested for loops if possible)?
The matrix looks like:
1.PNG
Can I make a matrix from the above matrix such that the rows and columns of the new matrix(or a cell array you can say) will be like you see below:
3.JPGthe empty cells can be NAN and the filled cells are the values of the 3rd column of the old matrix.

採用された回答

Walter Roberson
Walter Roberson 2019 年 12 月 30 日
result = accumarray(matrix(:,[2 1]), matrix(:,3), [], @(v) {v}, {nan})
However I wonder if you would be better off using
result = sparse(matrix(:,2), matrix(:,1), matrix(:,3))
which would be a sparse numeric array instead of a cell array that takes up about 108 bytes per entry.
  3 件のコメント
Andrei Bobrov
Andrei Bobrov 2019 年 12 月 30 日
[i,g] = findgroups(matrix(:,2));
out = flip(accumarray([matrix(:,1),i],matrix(:,3)),1);
Jay Vaidya
Jay Vaidya 2020 年 1 月 1 日
編集済み: Jay Vaidya 2020 年 1 月 1 日
That is what I am confused as well. If the input matrix(:,1:2) is of the order of mx2. I want to make a graph from this matrix. Lets say the order of the graph (look below) is pxq. Then, the order of the the result matrix should be pxq as well. In other words, I want to transform,
matrix->graph->adjacency matrix-> heatmap. My ultimate goal is to plot a heatmap from the input matrix. Mr. Bobrov, I have made a different thread for this, it would be great if you can help. https://www.mathworks.com/matlabcentral/answers/498570-how-to-convert-the-graph-below-in-to-heatmap

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

その他の回答 (2 件)

Jay Vaidya
Jay Vaidya 2019 年 12 月 31 日
Hello Mr. Roberson and Mr. Bobrov,
I am getting an error while running that. I have attached the snapshots of the error that says that SUBS must contain positive integer subscripts(associated with accumarray command) and another one that has an index value problem (associated with the sparse command). For convenience, I have attached the matrix.xlsx file that has the 'matrix' that is being used in this problem.
Attachment: matrix.xlsx (see below)
Thanks.
4.JPG

Andrei Bobrov
Andrei Bobrov 2019 年 12 月 31 日
T = readtable('path\to\your\xls\file\matrix.xlsx','ReadVariableNames',0);
T.Var3 = str2double(T.Var3);
T = T(any(T{:,1:2} ~= 0,2),:);
M = T{:,:};
[i,g] = findgroups(M(:,2));
out = flip(accumarray([M(:,1),i],M(:,3)),1);
  3 件のコメント
Andrei Bobrov
Andrei Bobrov 2019 年 12 月 31 日
T = readtable('C:\Octavework\forums\xls\matrix_v2.xlsx','Range','A:C','ReadVariableNames',0);
T.Var3 = str2double(T.Var3);
T = T(any(T{:,1:2} ~= 0,2),:);
M = T{:,:};
i = findgroups(M(:,2));
out = flip(accumarray([M(:,1),i],M(:,3)),1);
Jay Vaidya
Jay Vaidya 2019 年 12 月 31 日
So, the out matrix is of the order of 2550x2550. Where the graph is a 51(rows)x51(column) graph. Please see the image below. I want an 'out' matrix which has dimensions of 51x51 and represents just weights. In other words, the 'out' matrix will exactly the same as the graph in figure b.
7.JPG

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

カテゴリ

Help Center および File ExchangeSparse Matrices についてさらに検索

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by