# extract data based on specific condition

14 views (last 30 days)
Nazrin Afiq Abdul Rahman on 5 Jan 2021
Hello,
Can someone help me. i am newbie in looping and conditioning things in programming. I had some problem in getting the data based on specific criteria or condition. For example i have 2 set of data which i call set1 and set2 which each data consists of multiple row and 4 column. For full set of data, the total number of rows for both 2 set are different and the total number is not cosistent between both but have the same total number of column which is 4. For example, i show some of my 2 set data.
set1:
58202.0003472222 10 12 -0.00419000000000000
58202.0003472222 12 14 0.00259000000000000
58202.0003472222 14 20 0.00437000000000000
58202.0003472222 20 21 -0.00745000000000000
58202.0003472222 21 24 -0.00101000000000000
58202.0003472222 24 25 -0.000761000000000000
58202.0003472222 25 26 0.00243000000000000
58202.0003472222 26 29 -0.00121000000000000
58202.0003472222 29 32 0.00169000000000000
58202.0006944445 10 12 -0.00427000000000000
set2:
58202.0003472222 10 12 -0.00136000000000000
58202.0003472222 12 14 0.000256000000000000
58202.0003472222 14 20 -0.000641000000000000
58202.0003472222 20 21 0.00136000000000000
58202.0003472222 21 24 -0.000655000000000000
58202.0003472222 24 25 -8.90000000000000e-05
58202.0003472222 25 29 7.69000000000000e-05
58202.0003472222 29 31 1.86000000000000e-06
58202.0003472222 31 32 -0.00101000000000000
what i want to do is i wanna take the forth (4) column for both set of data and set it as variable example a for set1 and b for set2 with a condition that the column 1, column 2, and column 3 is the same or equal.
##### 2 CommentsShowHide 1 older comment
Nazrin Afiq Abdul Rahman on 5 Jan 2021
hello sriram,
the output should be the same as set 1 and set 2. There are 4 column and multiple row. Only data which meet the condition will be display or extract.
For example as u can see, set 1(1:6,1:3) and set2(1:6,1:3) the data was the same for both set. What i want is only the data at the column 4 which meet the condition (column1, column2 and column3 is the same for both set 1 and set 2).

Matt Gaidica on 5 Jan 2021
Here's an illustrative way to do it, for both the case where row order matters and does not:
set1 = [58202.0003472222,10,12,-0.00419000000000000;58202.0003472222,12,14,0.00259000000000000;58202.0003472222,14,20,0.00437000000000000;58202.0003472222,20,21,-0.00745000000000000;58202.0003472222,21,24,-0.00101000000000000;58202.0003472222,24,25,-0.000761000000000000;58202.0003472222,25,26,0.00243000000000000;58202.0003472222,26,29,-0.00121000000000000;58202.0003472222,29,32,0.00169000000000000;58202.0006944445,10,12,-0.00427000000000000];
set2 = [58202.0003472222,10,10,-0.00136000000000000;58202.0003472222,12,12,0.000256000000000000;58202.0003472222,14,14,-0.000641000000000000;58202.0003472222,20,20,0.00136000000000000;58202.0003472222,21,21,-0.000655000000000000;58202.0003472222,24,24,-8.90000000000000e-05;58202.0003472222,25,25,7.69000000000000e-05;58202.0003472222,29,29,1.86000000000000e-06;58202.0003472222,31,31,-0.00101000000000000;58202.0006944445,10,10,-0.00166000000000000;58202.0006944445,12,12,0.000414000000000000;58202.0006944445,14,14,4.67000000000000e-05;58202.0006944445,20,20,0.000736000000000000];
%% row order matters
a = [];
b = [];
% can't go past minimum row size
for ii = 1:min([size(set1,1),size(set2,1)])
if set1(ii,1) == set2(ii,1) && set1(ii,2) == set2(ii,2) && set1(ii,3) == set2(ii,3)
a = [a;set1(ii,4)];
b = [b;set2(ii,4)];
end
end
%% row order doesn't matter, compare all
a = [];
b = [];
% loop through all entires
for ii = 1:size(set1,1)
for jj = 1:size(set2,1)
if set1(ii,1) == set2(jj,1) && set1(ii,2) == set2(jj,2) && set1(ii,3) == set2(jj,3)
a = [a;set1(ii,4)];
b = [b;set2(jj,4)];
end
end
end
Your sample data isn't the best to try this out on, because I don't see a case that fits your condition for either method.
Nazrin Afiq Abdul Rahman on 7 Jan 2021
hello matt,
Yup. i had try both. first method the ouput was a bit less meanwhile second method takes time since it process by row on each column one by one. I try to modified the first method by adding 'find(variable)' and it looks fine. hehe. Thanks a lot matt for giving such idea :)