How to change the matrix size and value in matlab code?

2 ビュー (過去 30 日間)
Van Vy
Van Vy 2018 年 7 月 7 日
コメント済み: Van Vy 2018 年 7 月 7 日
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
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?
Van Vy
Van Vy 2018 年 7 月 7 日
Oh, Thank you so so much, that's all I need :) <3

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

採用された回答

Image Analyst
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.
  1 件のコメント
Van Vy
Van Vy 2018 年 7 月 7 日
It's so helpful <3 Thank you :)

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by