grouping structures together based on filter

조회 수: 5 (최근 30일)
hdg D
hdg D 2013년 8월 21일
Hi I have a structure that looks like the below
a
=
mydata:[135x6]
myid:{1x6}
mydates = [135x1]
I would like to separate out data into positive and negative I would go through each column in mydata and filter for positive and negative and match it to the corresponding dates
so that in the end i have 6 structures
so for example
apos(1)
=
mydata:[120x1]
mydates:[120x1]
myid ={1}
is there a clean way to do this rather than using a for loop?
Thanks, HD

채택된 답변

Walter Roberson
Walter Roberson 2013년 8월 21일
편집: Walter Roberson 2013년 8월 21일
aneg = cell2mat( arrayfun( @(K) struct( 'mydata', a.mydata(a.mydata(:,K) < 0,K), 'mydates', a.mydates(a.mydata(:,K) < 0), 'myid', a.myid(K)), 1:size(a.mydata,2),'Uniform', 0) );
apos = cell2mat( arrayfun( @(K) struct( 'mydata', a.mydata(a.mydata(:,K) > 0,K), 'mydates', a.mydates(a.mydata(:,K) > 0), 'myid', a.myid(K)), 1:size(a.mydata,2),'Uniform', 0) );
Question: what if the data is exactly 0?
  댓글 수: 3
Walter Roberson
Walter Roberson 2013년 8월 21일
Sorry, I fixed my typo.
If you want 0 to be included with positive, then use >= 0 instead of > 0 for apos
hdg D
hdg D 2013년 8월 21일
Sorry i figured it out thanks so much for your help!

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

추가 답변 (1개)

Azzi Abdelmalek
Azzi Abdelmalek 2013년 8월 21일
편집: Azzi Abdelmalek 2013년 8월 21일
%Example
a.mydata=randi([-10 10],135,6)
a.myid=randi([-10 10],1,6)
a.mydates =randi([-10 10],135,1)
%--------------------------------
c1=a.mydata(:);
c2=a.myid(:);
c3=a.mydates(:);
apos.mydata=c1(c1>=0)
apos.myid=c2(c2>=0)
apos.mydates=c3(c3>=0)

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by