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

조회 수: 5 (최근 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.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by