How to change the matrix size and value in matlab code?
5 次查看(过去 30 天)
显示 更早的评论
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 个评论
Image Analyst
2018-7-7
I don't know what your edit was, but I did help. My code in my answer below works. Did you try it?
采纳的回答
Image Analyst
2018-7-7
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!