필터 지우기
필터 지우기

How to save a column into separate variables

조회 수: 3 (최근 30일)
Fernando Pérez Lara
Fernando Pérez Lara 2018년 10월 18일
댓글: Fernando Pérez Lara 2018년 10월 18일
Hi, I want to save a column of a matriz into five variables, here is my code:
A = [1 0 0 0 -3 0;
0 1 0 0 -1 0;
0 0 1 0 -1 0;
0 0 0 1 -3 0];
I want to have into a, b, c, d and e the fifth column. Something like this:
a = 3
b = 1
c = 1
d = 3
Thank you!

채택된 답변

madhan ravi
madhan ravi 2018년 10월 18일
편집: madhan ravi 2018년 10월 18일
A = [1 0 0 0 -3 0;
0 1 0 0 -1 0;
0 0 1 0 -1 0;
0 0 0 1 -3 0];
a=A(:,1)
b=A(:,2)
c=A(:,3)
d=A(:,4)
e=A(:,5)
  댓글 수: 4
Stephen23
Stephen23 2018년 10월 18일
편집: Stephen23 2018년 10월 18일
"I thought that I could put"
[a, b, c, d] = X(:, 5)
Nope. X is one variable, a, b, c, and d are four separate variables.
You could convert X to a cell array and then use a comma separated list:
Z = num2cell(X(:,5));
[a,b,c,d] = Z{:}
But I would recommend just using indexing, exactly as madhan ravi's answer shows.
Fernando Pérez Lara
Fernando Pérez Lara 2018년 10월 18일
Thank you very much! Your code is working perfectly.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by