How can I convert 2 column matrix to a cell array?
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Jay Vaidya
am 30 Dez. 2019
Bearbeitet: Jay Vaidya
am 1 Jan. 2020
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](https://www.mathworks.com/matlabcentral/answers/uploaded_files/257617/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.JPG](https://www.mathworks.com/matlabcentral/answers/uploaded_files/257618/3.jpeg)
0 Kommentare
Akzeptierte Antwort
Walter Roberson
am 30 Dez. 2019
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 Kommentare
Andrei Bobrov
am 30 Dez. 2019
[i,g] = findgroups(matrix(:,2));
out = flip(accumarray([matrix(:,1),i],matrix(:,3)),1);
Weitere Antworten (2)
Andrei Bobrov
am 31 Dez. 2019
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 Kommentare
Andrei Bobrov
am 31 Dez. 2019
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);
Siehe auch
Kategorien
Mehr zu Data Distribution Plots finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!