Put matrix in cell

조회 수: 25 (최근 30일)
NA
NA 2018년 12월 10일
편집: Stephen23 2018년 12월 10일
I have this matrix T=[ 3 6 1 12 7 10 6 0 0 22 0 15;
2 4 4 3 2 4 5 0 0 20 0 4;
0 1 0 2 0 2 0 0 0 17 0 16;
0 5 0 6 0 7 0 0 0 6 0 0;
0 0 0 0 0 28 0 0 0 0 0 0]
I want to put each column of matrix T in cell c like this (the result should be)
c={[3,2], [6,4,1,5], [1,4], [12,3,2,6], [7,2], [10,4,2,7,28], [6,5],[ ],[ ],[22,20,17,6],[ ],[15,4,16]}
insted of zero column put [ ]
do not put zero in the cell

채택된 답변

Stephen23
Stephen23 2018년 12월 10일
편집: Stephen23 2018년 12월 10일
Simpler:
>> T = [3,6,1,12,7,10,6,0,0,22,0,15;2,4,4,3,2,4,5,0,0,20,0,4;0,1,0,2,0,2,0,0,0,17,0,16;0,5,0,6,0,7,0,0,0,6,0,0;0,0,0,0,0
28,0,0,0,0,0,0]
T =
3 6 1 12 7 10 6 0 0 22 0 15
2 4 4 3 2 4 5 0 0 20 0 4
0 1 0 2 0 2 0 0 0 17 0 16
0 5 0 6 0 7 0 0 0 6 0 0
0 0 0 0 0 28 0 0 0 0 0 0
>> F = @(v)v(v~=0).';
>> C = cellfun(F,num2cell(T,1),'uni',0);
>> C{:}
ans =
3 2
ans =
6 4 1 5
ans =
1 4
ans =
12 3 2 6
ans =
7 2
ans =
10 4 2 7 28
ans =
6 5
ans =
[]
ans =
[]
ans =
22 20 17 6
ans =
[]
ans =
15 4 16

추가 답변 (1개)

KSSV
KSSV 2018년 12월 10일
T=[ 3 6 1 12 7 10 6 0 0 22 0 15;
2 4 4 3 2 4 5 0 0 20 0 4;
0 1 0 2 0 2 0 0 0 17 0 16;
0 5 0 6 0 7 0 0 0 6 0 0;
0 0 0 0 0 28 0 0 0 0 0 0] ;
c={[3,2], [6,4,1,5], [1,4], [12,3,2,6], [7,2], [10,4,2,7,28], [6,5],[ ],[ ],[22,20,17,6],[ ],[15,4,16]} ;
A = T(:)' ;
ii = zeros(size(A));
jj = A > 0;
ii(strfind([0,jj(:)'],[0 1])) = 1;
idx = cumsum(ii).*jj;
out = accumarray( idx(jj)',A(jj)',[],@(x){x'});
  댓글 수: 1
NA
NA 2018년 12월 10일
the codes gives me this result
c={[3,2], [6,4,1,5], [1,4], [12,3,2,6], [7,2], [10,4,2,7,28,6,5], [22,20,17,6],[15,4,16]}
but I want this result
c={[3,2], [6,4,1,5], [1,4], [12,3,2,6], [7,2], [10,4,2,7,28], [6,5],[ ],[ ],[22,20,17,6],[ ],[15,4,16]} ;

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

카테고리

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