How can i write a code like this?

1 view (last 30 days)
Volkan Yangin
Volkan Yangin on 12 Oct 2016
Edited: dpb on 13 Oct 2016
Hi everbody,
I want to draw a linear change between two datas which have a difference as %5. For ex:
Datas:
-0,012 -0,024 -0,03 -0,02 -0,02 -0,01 -0,02 -0,03 -0,03 -0,05 -0,065 -0.06 -0.1
How can i create a linear change from -0.012 to -0.065 and from -0.065 to -0.1?
There are 512 datas and i can't correctly use if command. I use this command with first data, but i have to evaulate all of the datas because; for ex there may be a %5 difference between 126. and 225. datas.

Accepted Answer

dpb
dpb on 12 Oct 2016
Edited: dpb on 13 Oct 2016
Well, this may be a challenge depending on what you're really wanting, but as a starting point...
>> y=[ -0.012 -0.024 -0.03 -0.02 -0.02 -0.01 -0.02 -0.03 -0.03 -0.05 -0.065 -0.06 -0.1];
>> plot(y)
>> ix=find(abs(y-y(1))>0.05,1); % find first point that's >5% difference
>> [ix y(ix)] % what'd we find??? Looks like it...
ans =
11.0000 -0.0650
>> hold on,hL=line([1 ix],[y(1) y(ix)],'color','r'); % draw the line
>> find(abs(y(ix:end)-y(ix))>0.05,1)
ans =
Empty matrix: 1-by-0
>> max(abs(y(ix:end)-y(ix)))
ans =
0.0350
>>
Now you've hit a snag; your relative difference isn't met so the next attempt fails. So now is it the maximum difference or the last point or what?
Anyway, should give you some starting points to work from...just iterate over the pieces, remembering to keep adding the offset of the initial starting point in subsequent find results to refer to the position in the original vector or shorten the vector to match.
  2 Comments
dpb
dpb on 13 Oct 2016
Not sure I understand the question/problem...in the first segment it's the [ix y(ix)] pair; simply save those values as you progress down the initial vector however you choose to solve the issue regarding the problem the above runs into with the 0.05 magnitude difference.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by