Thread Subject: Indexing Problem

Subject: Indexing Problem

From: Ali Moradi

Date: 28 Feb, 2010 02:57:03

Message: 1 of 10

I've come across a small indexing problem and I'm hoping that you guys can help me. I have two 7x1 vectors, X & Y, where every three sets of points represent the coordinates of a right triangle, e.g. x(1), y(1), x(2), y(2), x(3), and y(3) give the three points of a right triangle (total of 3 triangles in the vectors). I'm simply trying to find one angle of the triangle by using arctan. This is a very simply calculation, but I have to do it many, many times. So I figured I could automate it by doing this in MATLAB:

B = zeros(3,1);
for i = 1:3:7
        B(i) = atand(abs(y(i+2)-y(i+1))/abs(x(i+1)-x(i)));
end


This is giving me the right numbers, but instead of creating a 3x1 vector, it's creating a 7x1 vector. I've tried doing several things, but nothing has proved useful. I can always write a couple of more lines that just creates a new 3x1 vector with the three useful elements from B, but I was hoping for a more straight forward method. If anyone has any suggestions, I'd really appreciate it! Thanks in advance.

- Ali Moradi

Subject: Indexing Problem

From: Ali Moradi

Date: 28 Feb, 2010 03:32:04

Message: 2 of 10

This is the only solution that I can think of that will solve the problem. I can always just add this after the loop:

B(B == 0) = [];

But I still would like to take care of this through the manipulation of the index, if possible. Thanks again.

Subject: Indexing Problem

From: TideMan

Date: 28 Feb, 2010 03:34:19

Message: 3 of 10

On Feb 28, 3:57 pm, "Ali Moradi" <kimus...@gmail.com> wrote:
> I've come across a small indexing problem and I'm hoping that you guys can help me. I have two 7x1 vectors, X & Y, where every three sets of points represent the coordinates of a right triangle, e.g. x(1), y(1), x(2), y(2), x(3), and y(3) give the three points of a right triangle (total of 3 triangles in the vectors). I'm simply trying to find one angle of the triangle by using arctan. This is a very simply calculation, but I have to do it many, many times. So I figured I could automate it by doing this in MATLAB:
>
> B = zeros(3,1);
> for i = 1:3:7
>         B(i) = atand(abs(y(i+2)-y(i+1))/abs(x(i+1)-x(i)));
> end
>
> This is giving me the right numbers, but instead of creating a 3x1 vector, it's creating a 7x1 vector. I've tried doing several things, but nothing has proved useful. I can always write a couple of more lines that just creates a new 3x1 vector with the three useful elements from B, but I was hoping for a more straight forward method. If anyone has any suggestions, I'd really appreciate it! Thanks in advance.
>
> - Ali Moradi

B = zeros(3,1);
k=0;
for iv = 1:3:7
         k=k+1;
        B(k) = atand(abs(y(iv+2)-y(iv+1))/abs(x(iv+1)-x(iv)));
end

Some gratuitous advice:
Bad practice using i as an index.
Next time you do complex arithmetic like this:
z=x + i*y;
You'll be screwed because i will no longer be sqrt(-1)

Subject: Indexing Problem

From: Ali Moradi

Date: 28 Feb, 2010 03:54:04

Message: 4 of 10

TideMan <mulgor@gmail.com> wrote in message <8833b86c-e37d-4c84-98d3-85cf3643d638@k2g2000pro.googlegroups.com>...
>
> B = zeros(3,1);
> k=0;
> for iv = 1:3:7
> k=k+1;
> B(k) = atand(abs(y(iv+2)-y(iv+1))/abs(x(iv+1)-x(iv)));
> end
>
> Some gratuitous advice:
> Bad practice using i as an index.
> Next time you do complex arithmetic like this:
> z=x + i*y;
> You'll be screwed because i will no longer be sqrt(-1)


Thank you and thank you! I didn't even think about the latter problem, I will make the appropriate changes to my code now and remove 'i' as my index.

Just as a note for the future, can you clarify what that 'k' is doing? I can see that it's kind of like a second index for B, but it's not changing from 1 to 2 to 3 in order to denote different elements of the B vector. I ran it and it obviously works, but I want to know *how* it works so I can properly utilize in the future. Thanks again for the help!

- Ali

Subject: Indexing Problem

From: TideMan

Date: 28 Feb, 2010 04:01:40

Message: 5 of 10

On Feb 28, 4:54 pm, "Ali Moradi" <kimus...@gmail.com> wrote:
> TideMan <mul...@gmail.com> wrote in message <8833b86c-e37d-4c84-98d3-85cf3643d...@k2g2000pro.googlegroups.com>...
>
> > B = zeros(3,1);
> > k=0;
> > for iv = 1:3:7
> >          k=k+1;
> >         B(k) = atand(abs(y(iv+2)-y(iv+1))/abs(x(iv+1)-x(iv)));
> > end
>
> > Some gratuitous advice:
> > Bad practice using i as an index.
> > Next time you do complex arithmetic like this:
> > z=x + i*y;
> > You'll be screwed because i will no longer be sqrt(-1)
>
> Thank you and thank you! I didn't even think about the latter problem, I will make the appropriate changes to my code now and remove 'i' as my index.
>
> Just as a note for the future, can you clarify what that 'k' is doing? I can see that it's kind of like a second index for B, but it's not changing from 1 to 2 to 3 in order to denote different elements of the B vector. I ran it and it obviously works, but I want to know *how* it works so I can properly utilize in the future. Thanks again for the help!
>
> - Ali

Take the semicolon off this line:
k=k+1
and you'll see that it IS changing from 1 to 2 to 3.

Subject: Indexing Problem

From: Ali Moradi

Date: 28 Feb, 2010 04:17:02

Message: 6 of 10

Okay, I see what you mean. So outside of a loop,

k = 0;
k = k + 1;

is simply an algebraic operation. But inside a loop it's a a sort of loop within itself, right? Even though it's not indexed with anything? That's very interesting. I didn't know that! I'm still trying to teach myself MATLAB's little tricks. Thank you again for your help, it was VERY helpful. :-D

- Ali

Subject: Indexing Problem

From: Matt Fig

Date: 28 Feb, 2010 04:39:02

Message: 7 of 10

"Ali Moradi" <kimusubi@gmail.com> wrote in message <hmcqnu$7dj$1@fred.mathworks.com>...
> Okay, I see what you mean. So outside of a loop,
>
> k = 0;
> k = k + 1;
>
> is simply an algebraic operation. But inside a loop it's a a sort of loop within itself, right? Even though it's not indexed with anything?



More like, inside the body of a FOR loop, you can do most any valid MATLAB operation, including addition (as here). k is simply a variable which has been used as a counter in the body of the loop. Note that one could also have used

k = 0;

then inside the loop, incremented after the assignment to B(k). Starting with zero has the advantage that when the loop is over, k holds the iteration count.

Subject: Indexing Problem

From: Matt Fig

Date: 28 Feb, 2010 05:12:05

Message: 8 of 10

"Matt Fig" <spamanon@yahoo.com> wrote in message
> More like, inside the body of a FOR loop, you can do most any valid MATLAB operation, including addition (as here). k is simply a variable which has been used as a counter in the body of the loop. Note that one could also have used
>
> k = 0;
>
> then inside the loop, incremented after the assignment to B(k). Starting with zero has the advantage that when the loop is over, k holds the iteration count.

Oops, I meant, of course, that one could have started with k = 1;

Subject: Indexing Problem

From: Ali Moradi

Date: 28 Feb, 2010 06:10:04

Message: 9 of 10

"Matt Fig" <spamanon@yahoo.com> wrote in message <hmctv5$1lb$1@fred.mathworks.com>...
> "Matt Fig" <spamanon@yahoo.com> wrote in message
> > More like, inside the body of a FOR loop, you can do most any valid MATLAB operation, including addition (as here). k is simply a variable which has been used as a counter in the body of the loop. Note that one could also have used
> >
> > k = 0;
> >
> > then inside the loop, incremented after the assignment to B(k). Starting with zero has the advantage that when the loop is over, k holds the iteration count.
>
> Oops, I meant, of course, that one could have started with k = 1;

I see what you mean, so 'k' is basically a counter. I'll put that in the back pocket for later use. The more I learn about MATLAB, the more fun it gets. Thanks again for the help!

- Ali

Subject: Indexing Problem

From: TideMan

Date: 28 Feb, 2010 06:12:36

Message: 10 of 10

On Feb 28, 7:10 pm, "Ali Moradi" <kimus...@gmail.com> wrote:
> "Matt Fig" <spama...@yahoo.com> wrote in message <hmctv5$1l...@fred.mathworks.com>...
> > "Matt Fig" <spama...@yahoo.com> wrote in message
> > > More like, inside the body of a FOR loop, you can do most any valid MATLAB operation, including addition (as here).  k is simply a variable which has been used as a counter in the body of the loop.  Note that one could also have used
>
> > > k = 0;
>
> > > then inside the loop, incremented after the assignment to B(k).  Starting with zero has the advantage that when the loop is over, k holds the iteration count.
>
> > Oops, I meant, of course, that one could have started with k = 1;
>
> I see what you mean, so 'k' is basically a counter. I'll put that in the back pocket for later use. The more I learn about MATLAB, the more fun it gets. Thanks again for the help!
>
> - Ali

To an old bugger like me (and I suspect Matt as well), all this is
patently obvious.
We learnt it not from Matlab but when learning Fortran back in the
60's.

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

rssFeed for this Thread

Contact us at files@mathworks.com