How to apply for loop for cell arrays?

조회 수: 1 (최근 30일)
Ali Y.
Ali Y. 2016년 6월 16일
댓글: Ali Y. 2016년 6월 16일
I have a matrix that I want dividing it to n matrices, say n = 2, and do some operations along their lengths. The second chain of ‘for’ loops does what I need when there is one matrix, as in the case of the parent matrix (a). But I can’t apply same procedure for the two cell arrays I have.
clear all
clc
a =(1000-0).*rand(10,2)+0 ;
[r,c] = size(a) ;
mina = [1 7] ;
maxa = [6 10] ;
b = cell(numel(mina),1) ;
for o = 1:numel(mina)
b{o} = a(mina(o): maxa(o),:);
end
max_a= zeros(r,c);
for i = 1:r
for j = 1:c
max_a(i,j) = max(a(1:i,j)) ;
end
end
This following lines was what I tried before but it doesn’t work.
for u = 1:numel(mina)
b2(1:maxa(u),:) = cell2mat(b(u)) ;
for i2 = mina(u):maxa(u)
for j2 = 1:c
max_a {u} = max(b2(1:i2,j2)) ;
end
end
end
  댓글 수: 2
Guillaume
Guillaume 2016년 6월 16일
With your given example, can you show what the output of your last loop should be, as I've no idea what you're trying to do with it.
Guillaume
Guillaume 2016년 6월 16일
Note that if you have a recent version of matlab, your first max_a is simply:
max_a = cummax(a);

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

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2016년 6월 16일
a =(1000-0).*rand(10,2)+0
[r,c] = size(a)
mina = [1 7] ;
maxa = [6 10] ;
b = cell(numel(mina),1) ;
for o = 1:numel(mina)
b{o} = a(mina(o): maxa(o),:);
end
for k=1:numel(b)
bb=b{k};
[r,c]=size(bb);
max_a= zeros(r,c);
for i = 1:r
for j = 1:c
max_a(i,j) = max(a(1:i,j)) ;
end
end
max_b{k}=max_a
end
  댓글 수: 1
Ali Y.
Ali Y. 2016년 6월 16일
Thank you Azzi for the help.

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

추가 답변 (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