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 Center および File ExchangeMatrix Indexing についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by