convert a 10x2 matrix into x,y coordinates

조회 수: 21 (최근 30일)
Supriya Gain
Supriya Gain 2019년 8월 7일
댓글: Stephen23 2022년 12월 14일
I have a 10X2 matrix. Now i want to convert this into x,y co-ordinate. like (x,y)=[(1,1), (1,2)]. How to do that?
  댓글 수: 3
KALYAN ACHARJYA
KALYAN ACHARJYA 2019년 8월 7일
@Madhan as per my undestanding, may be this one
mat_cor{i,j}=[i,j]; % The loop creates it
A structure with [1,1], [1,2]..... same size as matrix size. I have no clue, WHY?
Supriya Gain
Supriya Gain 2019년 8월 7일
Suppse i have a 3X2 matrix [1 2;3 4;5 6]
Now i want the answer as (x1,y1)=(1,2)
(x2,y2)=(3,4)
(x3,y3)=(5,6)

댓글을 달려면 로그인하십시오.

채택된 답변

Stephen23
Stephen23 2019년 8월 7일
>> M = [1,2;3,4;5,6]
M =
1 2
3 4
5 6
>> V = 1:size(M,1);
>> fprintf('(x%d,y%d)=(%d,%d)\n',[V;V;M.'])
(x1,y1)=(1,2)
(x2,y2)=(3,4)
(x3,y3)=(5,6)
  댓글 수: 2
Sirvan Ahmadi
Sirvan Ahmadi 2022년 12월 14일
What if we only want to have,
(1,2)
(3,4)
(5,6)
Stephen23
Stephen23 2022년 12월 14일
"What if we only want to have"
M = [1,2;3,4;5,6]
M = 3×2
1 2 3 4 5 6
fprintf('(%d,%d)\n',M.')
(1,2) (3,4) (5,6)

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Steven Lord
Steven Lord 2019년 8월 7일
If you are trying to create twenty individual variables, one for each element of your 10-by-2 matrix, DON'T DO THAT. See this Answers post for a lengthy discussion of why this is strongly discouraged.
In my opinion, turning that one variable into two (to make it easier to refer to the X and Y coordinates separately) seems reasonable.
A = [1 2;3 4;5 6];
x = A(:, 1);
y = A(:, 2);
With this approach, what you called x2 would instead be x(2). Or you could use the whole vector at once.
plot(x, y) % Shorter than but equivalent to
plot(A(:, 1), A(:, 2))
But the proliferation of variables from one to twenty-one (the twenty new ones plus the original one) is just going to clutter your workspace. Consider: if you had a 10000-by-2 matrix instead of a 10-by-2, do you want to search through twenty thousand variables in your workspace to access the piece of data you need?

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by