While loop to detect 0 slope

조회 수: 2 (최근 30일)
Neil
Neil 2014년 6월 4일
댓글: Star Strider 2014년 6월 4일
Hello everyone,
so I'm currently trying to make a code that will separate some data. To do so, I came up with a code that I thought would work, and I've tried looking at the what the error message mean, but I cant understand. My code is the follwoing
*g=0; j=0; A=[5 5]; h=0; slope=5; while (g<1785) & (A(1,1)>0) & (h<0.15) h = AverageStrain(1+g:20+g,:); i = AverageStress(1+g:20+g,:); A = polyfit(h,i,1); j = g+10; g = g+20; end
Cycle1UpStrain=AverageStrain(0:j,1);*
The error message i receive is ;
*Subscript indices must either be real positive integers or logicals.
Error in MTSAnalyser (line 43) Cycle1UpStrain=AverageStrain(0:j,1);*
here, the files AverageStress and AverageStraain are 1785x1 variables. I tried doing a linear fit of 20 data points at the time so that when the linear slope calculated is 0 are negative, it stops and extracts the number j.
j would be used to create a variable made of a fraction of AverageStress and AverageStrain, data points 1 to j. (afterwards I would make it something like j to a new variable limit)
Also, my value of A always ends in something like [574, -64], which is weird since I wanted it to stop at a slope equal 0 or smaller, which is the first row, first column of A.
I'm quite new to Matlab, so try not to assume I know a whole bunch.
Thank you for your help! N
  댓글 수: 1
Star Strider
Star Strider 2014년 6월 4일
Formatted code:
g=0;
j=0;
A=[5 5];
h=0;
slope=5;
while (g<1785) & (A(1,1)>0) & (h<0.15)
h = AverageStrain(1+g:20+g,:);
i = AverageStress(1+g:20+g,:);
A = polyfit(h,i,1);
j = g+10;
g = g+20;
end

댓글을 달려면 로그인하십시오.

채택된 답변

Star Strider
Star Strider 2014년 6월 4일
Here’s the problem:
Cycle1UpStrain=AverageStrain(0:j,1);
Zero is not a positive integer. Your (variable or index) ‘j’ is also defined as zero, as best I can see from your code (difficult to read because it’s not formatted).
If you want to find the indices where AverageStrain is equal to zero and one respectively, try this:
AvStn0Idx = find(AverageStrain == 0);
AvStn1Idx = find(AverageStrain == 1);
  댓글 수: 2
Neil
Neil 2014년 6월 4일
Thanks, the mistake was in fact the 0 there. it needed to start at 1. Meanwhile, my A still ends at a value of [571, -644]. Ideas?
Star Strider
Star Strider 2014년 6월 4일
I’m not entirely understand what you’re doing, but I see some problems. One is that h is a vector, not a scalar (at least as I read your code), so if any value of h is less than 0.15, that test will be true.
Consider:
h = [0.1 randi(10,1,5)];
htest = h < 0.15
yields:
htest =
1 0 0 0 0 0
You may want to consider how you’re testing h. What information do you really want from it? First value? Last value? Mean? Norm? Minimum? Maximum?
I would solve the h problem (if it is a problem) first, then see if your while loop runs as it should.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by