problem in cell matrix operations

조회 수: 1 (최근 30일)
Pooneh Shah Malekpoor
Pooneh Shah Malekpoor 2021년 5월 14일
답변: Walter Roberson 2021년 5월 14일
Hello
I have code like this which gives out a 5*1 cell matrix:
row = [0; 1; 2; 3; 4] ;
Coh = [15;16 ;17; 18; 20;19;7;8;22;25] ;
N = length(A) ;
Csurf = cell(N,1) ;
for i = 1:N
if row(i) == 0
Csurf{i} = 0 ;
else
Csurf{i} = Coh(1:row(i)) ;
Coh(1:row(i)) = [] ;
end
end
end
----------------------------------------------------
Now imagine that both row and Coh become cell matrices by the below repmat command,
Nmc=2
BB = repmat({row},Nmc,1);
CV=repmat({Coh},Nmc,1);
I have to write a code which considers the first cell of both BB and CV together and do the calculations as in the first code and then consider the second cell of BB and CV together and do the same calculations. The result would be a 5*2 cell matrix as Nmc=2. How should i formulate this?should i define a for loop? i am not good at cell indexing!
Any help is highly appreciated.
  댓글 수: 1
Adam Danz
Adam Danz 2021년 5월 14일
Please edit your question to format your code using the text/code toggle feature of the rich text editor.

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

채택된 답변

Walter Roberson
Walter Roberson 2021년 5월 14일
Your current code would take 1 row from Coh, then the following 2 rows, then the following 3 rows, and so on. Is that what is desired?
If it is, then consider using mat2cell() . mat2cell() even handles counts of 0.
row = [0; 1; 2; 3; 4] ;
Coh = [15;16 ;17; 18; 20;19;7;8;22;25] ;
mat2cell(Coh, row, 1)
ans = 5×1 cell array
{0×1 double} {[ 15]} {2×1 double} {3×1 double} {4×1 double}

추가 답변 (0개)

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by