Tags: array, arrayof, convinced, corresponding, differences, matlab, ofpercent, percentage, price, programming, stock, values
find percentage differences in array
On Programmer » Matlab
1,534 words with 1 Comments; publish: Mon, 12 May 2008 10:32:00 GMT; (20062.99, « »)
I am convinced there is a simple way to do this. If I have an array
of, say, stock values and I want to get a corresponding array of
percent price improvements -- can I do this in one command.
For example.
A = [1 2 2 3 2 1 1 1 2]';
and I want:
B = [100%; 0%; 50%; -33%; etc . . .];
I have
EDU>> A = [1 2 2 3 2 1 1 1 2]';
EDU>> (diff(A)./A(1:end-1))'
ans =
1 0 0.5 -0.33 -0.5 0 0 1.0000
any better ideas?
thanks,
Tim
http://matlab.itags.org/q_matlab_17862.html
All Comments
Leave a comment...
- 1 Comments

- Tim Booher wrote:
>
> I am convinced there is a simple way to do this. If I have an array
> of, say, stock values and I want to get a corresponding array of
> percent price improvements -- can I do this in one command.
> For example.
> A = [1 2 2 3 2 1 1 1 2]';
> and I want:
> B = [100%; 0%; 50%; -33%; etc . . .];
> I have
> EDU>> A = [1 2 2 3 2 1 1 1 2]';
> EDU>> (diff(A)./A(1:end-1))'
> ans =
> 1 0 0.5 -0.33 -0.5 0 0 1.0000
> any better ideas?
Why do you need a better idea?
This works reasonably. It uses no
unnecessary loops. Its as efficient
as possible. Its written in one
line of code. In fact, its what
anyone else would suggest as the
way to do this.
Expend your energy in worrying
about the real problems in your
code. Use the profiler (and mlint)
to help identify the problems when
you are not sure.
John
#1; Mon, 12 May 2008 10:33:00 GMT