How to change the matrix size and value in matlab code?
5 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hi everyone,
I'm a newbie in matlab. I have a question and hope to receive your surpport.
I have a matrix 3 columns, n rows, like this:
(column 1 and 2 are integer, column 3 is real number)
1 - 2 - a12,
1 - 3 - a13,
2 - 1 - a21,
2 - 3 - a23,
3 - 3 - a33,
4 - 1 - a41,
4 - 2 - a42,
Now I want to change the matrix above become this matrix: (means that the value of column 1 and column 2 about will become the index of new matrix, if don't have index in some position, the new matrix value at that position will be blank)
NaN - a12 - a13,
a21 - NaN - a23,
NaN - NaN - a33,
a41 - a42 - NaN,
Have anyone help me? I'm looking forward your respond so much :)
2 commentaires
Image Analyst
le 7 Juil 2018
I don't know what your edit was, but I did help. My code in my answer below works. Did you try it?
Réponse acceptée
Image Analyst
le 7 Juil 2018
This will do it:
% Sample data.
m1 = [...
1 , 2 , 100,
1 , 3 , 101,
2 , 1 , 102,
2 , 3 , 103,
3 , 3 , 104,
4 , 1 , 106]
[rows, columns] = size(m1)
% Figure out how many rows and columns we'll need in the output array.
uniqueRows = unique(m1(:, 1))
uniqueCols = unique(m1(:, 2))
% First create an array of all nans.
output = nan(max(uniqueRows), max(uniqueCols))
% Now go through the input array placing the values
% into the output array at the correct location.
for row = 1 : rows
thisRow = m1(row, 1);
thisCol = m1(row, 2);
output(thisRow, thisCol) = m1(row, 3);
end
output % Print to command window.
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Logical dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!