How do I make matlab read certain lines and do calculations only when a condition is true?
Show older comments
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
6 Comments
Abraham Boayue
on 24 Feb 2018
Hi Dylan, your problem seems pretty simple, but I'm not sure if I understand what you want. If Z is the condition, then it has values between 5 and -4 including 0 and 1 for ex1.csv. If Z = 1, you will have a single acceleration for t = 5s and v= 6m/s which will also be the case for ex2.csv.In both cases, the acceleration is a scaler value function. Is this what you want? Cause that's how I understand your question. You can get an acceleration vector for all positive values of Z including or not including 0.
Dylan Mecca
on 26 Feb 2018
Bob Thompson
on 26 Feb 2018
av is an array, while f I'm assuming is a single value. Unless avv is a cell array then you cannot put an entire array, av, into a single value, f. That's why you're getting the error.
Dylan Mecca
on 26 Feb 2018
Bob Thompson
on 26 Feb 2018
Does avv need to be 20x1? You could just initiate the size of avv to be based on f. Additionally, there still needs to be agreement between av and f. Even if you have two arrays if you have one that's 18x1 and one that is 16x2 then Matlab is not going to want to join them.
Dylan Mecca
on 26 Feb 2018
Accepted Answer
More Answers (1)
Abraham Boayue
on 26 Feb 2018
Edited: James Tursa
on 26 Feb 2018
t = 0.1:0.1:2.1;
v= 1:21;
z = [ones(1,8) 0 ones(1,6) 0 0 ones(1,4)];
N=length (t)
a = zeros(1,N);
A1 = [];
A2 = [];
A3 = [];
for i =1:N
if(z==1 && v <=8) % condition
% for the
% 1st sect.
a(i) = v(i)/t(i);
A1 = [A1 a];
elseif ( z==1 ||v <=15)
a(i) = v(i)/t(i);
A2 = [V2 a];
elseif (z==1)
a(i)= v(i)/a(i);
A3 = [A3 a];
end
end
dis (' a for the first section ')
disp(A1)
disp('a for the second section )
disp(A2)
disp(' and a for the last section)
disp(A3)
4 Comments
Abraham Boayue
on 26 Feb 2018
Hey Dylan, Why don't you take a look at this simple code I sent; it does the job, although I haven't tested it as I do not have access to my laptop to test it. The data that you are dealing with can be entered into matlab by hand and thereby making life easier. I can make it more advanced than that if you want but that may take a few days from now. I wrote this one on my phone. Hope that helps.
Dylan Mecca
on 26 Feb 2018
Edited: Dylan Mecca
on 26 Feb 2018
Abraham Boayue
on 26 Feb 2018
Yes, I do have an idea. I just to have access to pc at this point, I will look into that when I do.
Dylan Mecca
on 26 Feb 2018
Categories
Find more on Creating and Concatenating Matrices 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!