How do I pull Time values from the found indicies of Force?
1 次查看(过去 30 天)
显示 更早的评论
Hello, I am trying to find the force values for less than 10N . Then I need to use the Max and Min indicies found from force < 10N. Using those indicies I need to find the two corresponding time values/indicies. Then I need to find the difference in time between the max and min.
The imported data is 2 colums of equal rows of data labeled Time(s), and Force(N).
close all; clear all; clc;
GRF1 = importdata('GRF_1.txt'); %Imports data from selected text file.
time = GRF1.data(:,1); %takes the time values from colum 1 and stores them into a variable named time.
force = GRF1.data(:,2); %takes the force values from column 2 and stores them into a variable named force.
indBefore10 = find(force<10); %Finds all indicies which the force value is <10 .
[maxForce10, maxIndForce10] = max(indBefore10) %finds the MAX force, and its corresponding index.
[minValForce10, minIndForce10] = min(indBefore10); %finds the MIN force, and its corresponding index.
MinTimeInd = find(time == minIndForce10) %I think this takes the MIN force index found and finds corresponding time value from index.
MaxTimeInd = find(time == maxIndForce10) %I Think this takes the MAX force index found and finds corresponding time value from index.
TimeDiff = MaxTimeInd - MinTimeInd %Should calculate the difference between the max and min time values found.
So. The problem I am having is that the when I try to use the Force index in time I get a value which doesnt correspond to the correct index. Also my MaxTimeInd is returning and empty colum vector. Finally, I notice that "indBefore10 = find(force<10);" is returning a long list of values which exceed 10. Any help to figure this out would be greatly appreciated.
0 个评论
采纳的回答
Thiago Henrique Gomes Lobato
2020-3-15
编辑:Thiago Henrique Gomes Lobato
2020-3-15
indBefore10 contains only the index, not the force itself, you have to give then back to the force array to actually get the result, that's why max(indBefore10) will give you unreasonable results. You also have more a couple of errors in your code that are based in a confusion between index/value. A way to do it that works is:
indBefore10 = find(force<10); %Finds all indicies which the force value is <10 .
[maxForce10, maxIndForce10] = max(force(indBefore10)) ; % Get the force values belonging to the indices
[minValForce10, minIndForce10] = min(force(indBefore10));
% Correct index (since they are founded based in the indBefore10
maxIndForce10 = indBefore10(maxIndForce10);
maxIndForce10 = indBefore10(minIndForce10);
MinTimeInd = time(minIndForce10) ; % time is most probably not integer, so time==Index will give you wrong results. This way will always work
MaxTimeInd = time(maxIndForce10);
TimeDiff = MaxTimeInd - MinTimeInd
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!