create sub arrays from a array

Hello Everyone,
I am new to MATLAB so I am asking a very basic question which I am not able to solve.
I have an array of diemensions 672*9 , I want to create smaller arrays by comapring the value of column 9 only.
So for example my values in column 9 are
4
4
4
3.5
3
3.1
3.2
4
3.5
I specify the threshold value as 3.4 . So as my values in first 4 rows is more than 3.4 I store them in a new array. Then next 3 values in second array and finally last 2 values in third array.
I hope I have framed the question properly.

 채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2013년 6월 5일
편집: Azzi Abdelmalek 2013년 6월 5일

0 개 추천

Edit
a=[4,4,4,3.5,3,3.1,3.2,4,3.5];
idx=a>=3.4;
idx2=sort([strfind(idx,[true false]) strfind(idx,[false,true]) numel(a)]);
idx1=[1 idx2(1:end-1)+1];
% pre-allocate;
y=cell(1,numel(idx));
for k=1:numel(idx1)
y{k}=a(idx1(k):idx2(k));
end
y{1}
y{2}
y{3}

댓글 수: 6

Azzi Abdelmalek
Azzi Abdelmalek 2013년 6월 5일
Look at edited answer
Jan
Jan 2013년 6월 5일
편집: Jan 2013년 6월 5일
What about pre-allocating y, because you know the final length initially? When you add this details, it is more likely that the OP will not need to ask this again next week.
It is recommended not to use the loop counter k outside the loop in Matlab.
I'm convinced, that the forum is really a good place to offer good programming practize.
Azzi Abdelmalek
Azzi Abdelmalek 2013년 6월 5일
Ok, it's done
Ricky
Ricky 2013년 6월 5일
Actually the size of my array is 672*9 and I want to evaluate only row 9 for the condition. So I would actually have 25 or 26 arrays with 9 columns. These arrays are the subset of the main array. So if I add all the rows in these 25 or 26 arrays I will get 672 rows of initial array.
Jan
Jan 2013년 6월 5일
@Ricky: I do not understand your explanations. Does the method shown by Azzi help to solve your problem?
Ricky
Ricky 2013년 6월 5일
@Jan Simon : Sorry I misunderstood the solution it works for me now.

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

추가 답변 (1개)

Andrei Bobrov
Andrei Bobrov 2013년 6월 6일

0 개 추천

a=[4,4,4,3.5,3,3.1,3.2,4,3.5];
out = accumarray(cumsum([true; diff(a(:) > 3.4)~=0]),a(:),[],@(x){x});

카테고리

도움말 센터File Exchange에서 Performance and Memory에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by