how to split data with 'or' condition ?

1 view (last 30 days)
pruth
pruth on 26 Nov 2019
Commented: pruth on 26 Nov 2019
I find this problem bit ticky. not sure how to explain. i hope you'll undersatnd.
I have created this file tempdata_O4_month.mat (attached )
in this file the fourth column is angles.
so in fourth column there is set of repeatating angles(scan) which are 1 ,2,3,5,10,20,40,90 and sometimes it starts from 0. i.e. 0, 0.5, 1, 2, 3, 5, 10,20, 40,90
here i dont need 90 degree row, but i need other.
i want to write a code which will split all the rows asscociated with these set of angles and make a cell.
earlier i was uusing this code
idx = cumsum(all(tempdata_o4(:,4)==90,2)); % to split rows at 90 degree
fun = @(r){tempdata_o4(r(2:end),:)};
QDOAS = accumaracy(idx,rows(:),[],fun);
QDOAS = QDOAS(~cellfun('isempty',QDOAS));
i thought it gives exactly i wanted but i realized that the 90 degree is not always avaialble after each set of scans to split the rows, which creates a cell with two sets of angles. this is not what i want.
i want to use this condition then, ''whenever the code finds the set of only these angles in forth coulmn i.e (1,2,3,5,10,20,40 or 0, 0.5, 1, 2, 3, 5,10,20,40) make it a cell out of it just like i did in above code.'' also i need to use one more condition ''Some times the angles are like 1,2,1,2,3,5,10,20,40 or may be any angle is missing i.e. 1,2,10,20,40 so here just skip this set and go to next set , no need to consider it ''
this problem is killing me since last night. hope anybody of you can help me !

Accepted Answer

Andrei Bobrov
Andrei Bobrov on 26 Nov 2019
Edited: Andrei Bobrov on 26 Nov 2019
EDIT
lo = [diff(tempdata_o4(:,4)) > 0;false];
i = cumsum(diff([false;lo]) == 1).*lo;
C = accumarray(i + 1,(1:numel(i))',[],@(x){sortrows(tempdata_o4(x,:),4)});
p = {[1;2;3;5;10;20;40];[0;.5;1;2;3;5;10;20;40]};
j = cellfun(@(x)(size(x,1)==7 && all(ismember(p{1},x(:,4)))) ||...
(size(x,1)==9 && all(ismember(p{2},x(:,4)))),C) ;
C_out = C(j);
  2 Comments
pruth
pruth on 26 Nov 2019
yes .... it worked perfect! thank you ....i made some changes...but it worked!! thank you so much !

Sign in to comment.

More Answers (1)

Guillaume
Guillaume on 26 Nov 2019
Edited: Guillaume on 26 Nov 2019
My recommendation would be not to split your data into cell arrays. You're complicating your life for no reason.
First, you should convert your matrix into a table, or possibly a timetable since it looks like your first column is date. So:
tempdata = array2table(tempdata_04, 'VariableNames', {'Date', 'xxx', 'yyy', 'Angle', 'zzz', 'ttt'}); %no idea what some columns represent use appropriate names
tempdata.Date = datetime(tempdata.Date, 'ConvertFrom', 'datenum');
Already your data is easier to visualise and more importantly easier to work with. You now have access to functions such as groupsummary which allows you to apply the same calculation to different groups (e.g. each mean of the all the variable per day, or month, or max of the 5th column for each angle, etc.) in just one line of code or groupfilter which allows you to remove some date from the table according to some criteria, again in just one line of code.
It's unclear what you're actually doing with your data, but splitting it into several variables is probably not the correct approach.
  1 Comment
pruth
pruth on 26 Nov 2019
sir, thanks for reply . I am running the code throgh some computer model. and i need to have cell array for that. i need a file as it is .

Sign in to comment.

Categories

Find more on Variables 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!