MATLAB Answers

0

How can I search data for various conditions and if conditions are met (more than once) assign variables?

Asked by Trude Mashburn on 11 Jun 2018
Latest activity Edited by Trude Mashburn on 12 Jun 2018
The Details: I have a matrix of input data that I read into a script. The matrix is large e.g 70000 for row and 3 for column. Input = matrix(70000, 3) Column 1 is time Column 2 is A Colum 3 is B
I have a script set up (see below). I need the script to read the data file and check each row of Column 2 and 3 for a condition. If it meets that condition I set the variable ACond1, ACond2, BCond1 and/or BCond2 equal to the time that is associated with that Condition. The problem: There are multiple occurrences (times) that ACond1, ACond2, BCond1 and/or BCond2 and of these only select occurrences will be ACond1_e, ACond2_e, BCond1_e, and BCond2_e.
A working script with the example data shown below would yield the following: ACond1 (1) = 2.00 ACond1(2) = 17.00 ACond1(m) = ACond2(1) = 6.00 ACond2(2) = 22.00 ACond2(m) BCond1(1)= 8.00 BCond1(2) = 24.00 BCond1(m) BCond2(1) = 14.00 BCond2(2) = 30.00 BCond2(m)
Typically ACond1 would occur before ACond2 and BCond1 would occur before BCond2 and In addition the ACondition would occur before BCondition.
*********************** Example of the data is as follows: time = [1.00, 2.00, 3.00, 4.00, 5.00, 6.00, 7.00, 8.00, 9.00, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0,17, 18, 19, 20, 21, 22, 23,24, 25, 26, 27, 28, 29, 30, 31,32];
A = [2, 3, 3, 3, 3, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 2, 3, 3, 3, 3, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7];
B = [1, 1, 1, 1, 1, 1, 1, 3, 3, 3, 3, 3, 3, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 3, 3, 3, 3, 3, 3, 2, 2, 2];
%--Read file. filename = 'sample.csv'; disp('start file read'); data = csvread(filename); disp('end file read');
time = data(:,1); A = data(:,2); B = data(:,3);
% Flags for Events. % A = 3; % A in condition 1 % A = 6; % A in condition 2 % B = 3; % B in Condition 2 % B = 2; % B in Condition 1
% Set up (initialize) counters for the If Statements below c = 0; d = 0; e = 0; f = 0; % % Set up zero vectors ACond1 = zeros(size(c)); ACond2 = zeros(size(d)); BCond1 = zeros(size(e)); BCond2 = zeros(size(f));
% Initializing some variables n = 1; BCond2 = 0;
% If statements search each row of input (time, A and B and % identify the time for the first occurrence of ACond1, ACond2, BCond1 and BCond2
for i = 1:max(size(time)) if A(i) == 3 c = c + 1; for p = 1:max(size(c)) ACond1(1,c) = time(i); end; ACond1_e = min(ACond1()); end; if A(i) == 6 d = d + 1; for q = 1:max(size(d)) ACond2(1,d) = time(i); end; ACond2_e = min(ACond2()); end; if B(i) == 3 e = e + 1; for r = 1:max(size(e)) BCond1(1,e) = time(i); end; BCond1_e = min(BCond1()); end; if B(i) == 2 f = f + 1; for s = 1:max(size(f)) BCond2(1,f) = time(i); end; BCond2_e = min(BCond2()); end; if (time(i) > BCond2) n = n + 1; end; end; i = i - 1; end

0 Answers