problem in cell matrix operations
2 views (last 30 days)
Show older comments
Pooneh Shah Malekpoor
on 14 May 2021
Answered: Walter Roberson
on 14 May 2021
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 Comment
Adam Danz
on 14 May 2021
Please edit your question to format your code using the text/code toggle feature of the rich text editor.
Accepted Answer
Walter Roberson
on 14 May 2021
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)
0 Comments
More Answers (0)
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!