MATLAB: How to make matlab read certain lines and do calculations only when a condition is true

calculation when condition is true

Please see attached file. variables t and v represent time and velocity respectively. Z being the condition. I want to calculate average acceleration over the points where the condition is '1', and not when the condition is '0'.
I also want the calculation to be specific to the '1' sections. In other words, calculate the average acceleration for the first section where z is 1, and make that a vector value. Then calculate a separate average acceleration for the second section where z is '1', and so on for the rest of the data.
clear;
clc;
%----------------------------------------------------
uiimport %importing said csv file
while (1) %keeps code from running while selecting a file
cont=input('Press 1 then enter to continue:','s');
if cont=='1'
break
end
end
wrdvec = size(t);
a = repmat('1',[wrdvec,1]);
b = int2str(z)
abc = a==b
av = v(2:end,:) - v(1:end-1,:)
at = t(2:end,:) - t(1:end-1,:)
av = av./at;
avv = zeros(20,1);
for f = find(abc)
avv(f) = av
end

Best Answer

clear variables
% Test the loop with these values of t, v and z, it should work just fine
% for any data.
t = 1:10;
v = [ 3 4 5 7 3 2 8 2 1 6];
M = length(t);
z = [1 1 0 1 1 0 1 0 1 1];
A = [];
for k = 1:M
if(z(k)== 1)
a = v(k)/t(k);
A = [A; k a];
fprintf('A section (eg. 1 and 2) occurs after a number skip :counter of 1s before a 0 = %12.8f, a = %12.8f\n', k, a);
end
continue
end
%disp(A);