Conversion of a matrix into multiple column vectors

조회 수: 140 (최근 30일)
Subham Burnwal
Subham Burnwal 2015년 3월 11일
댓글: Alpha Bravo 2020년 4월 20일
If we have a matrix A=[1 2 ; 3 4 ; 5 6] then it will be converted to vectors like x1= [1 3 5] x2= [2 4 6]

채택된 답변

Stephen23
Stephen23 2015년 3월 11일
편집: Stephen23 2020년 4월 20일
If you know how many columns you want, then you can simply assign them in code like this:
>> A = [1,2;3,4;5,6];
>> X1 = A(:,1);
>> X2 = A(:,2);
If you have many columns, or an unknown sized matrix, then you can split it up using num2cell with the second optional argument:
>> B = num2cell(A,1);
>> B{1}
ans =
1
3
5
>> B{2}
ans =
2
4
6
Accessing the contents of the cell array is simple and efficient using indexing:
Accessing dynamically named variables like x1, x2, x3, etc. is not recommended, because it forces you into writing slow, complex, obfuscated, buggy code that is hard to debug. Read this to know why:
  댓글 수: 5
Stephen23
Stephen23 2020년 4월 20일
"is the code above correct?"
I don't know. What do you expect that code to do?
Alpha Bravo
Alpha Bravo 2020년 4월 20일
kindly;
  1. reduce m x n matrix to its frequency count
  2. crosstab into 2x 2
  3. subject those to compute chi2 and p
  4. interpret that result
  5. that is, to find the chi2, and p of the given matrix; the goodness of fit test

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

추가 답변 (1개)

Giorgos Papakonstantinou
Giorgos Papakonstantinou 2015년 3월 11일
편집: Giorgos Papakonstantinou 2015년 3월 11일
x1 = A(:,1).';
x2 = A(:,2).';

카테고리

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