Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
index manipulation... shifting and rotating

Subject: index manipulation... shifting and rotating

From: Anthony Hopf

Date: 25 May, 2010 01:54:03

Message: 1 of 9

I know there are functions like rot90 to rotate a matrix about a point, but you need the the matrix. I only have the linear index values of the matrix. I can easily shift the indexes by adding or subtracting either row or column values... but what if I wanted to rotate by 90 or 180 degrees... is this possible with just the index values?

-one idea I had was to progressively subtract (or add depending on which way I want to shift) more from each row index, but I don't have them in a specific order to implement this progressive shift.

here is a small example:

A = [ 11 22 33 44 55; 66 77 88 99 1010]

idx(1,1) = [1 6 ]
idx(1,2) = [2 7 ]
idx(1,3) = [3 8]
idx(1,4) = [4 9]
idx(1,5) = [5 10]

and get

A_shift = [55 44 33 22 11; 1010 99 88 77 66] without ever seeing A... but knowing its size and dimensions.

confusing? thank you for the help... I know I've been asking quite a few questions lately but I am learning a lot!!

Subject: index manipulation... shifting and rotating

From: TideMan

Date: 25 May, 2010 02:18:41

Message: 2 of 9

On May 25, 1:54 pm, "Anthony Hopf" <anthony.h...@gmail.com> wrote:
> I know there are functions like rot90 to rotate a matrix about a point, but you need the the matrix.  I only have the linear index values of the matrix.  I can easily shift the indexes by adding or subtracting either row or column values... but what if I wanted to rotate by 90 or 180 degrees... is this possible with just the index values?
>
> -one idea I had was to progressively subtract (or add depending on which way I want to shift) more from each row index, but I don't have them in a specific order to implement this progressive shift.
>
> here is a small example:
>
> A = [ 11 22 33 44 55; 66 77 88 99 1010]
>
> idx(1,1) = [1 6 ]
> idx(1,2) = [2 7 ]
> idx(1,3) = [3 8]
> idx(1,4) = [4 9]
> idx(1,5) = [5 10]
>
> and get
>
> A_shift = [55 44 33 22 11; 1010 99 88 77 66] without ever seeing A... but knowing its size and dimensions.
>
> confusing?  thank you for the help... I know I've been asking quite a few questions lately but I am learning a lot!!

I'm afraid your example makes no sense to me at all.
For example, what does:
idx(1,1)=[1 6]
mean?
There is one slot on the LHS, but 2 slots on the RHS, so that can't
work.

But to get A_shift from A, you don't need to screw around like that,
just do:
A_shift=fliplr(A);

Subject: index manipulation... shifting and rotating

From: Walter Roberson

Date: 25 May, 2010 02:22:11

Message: 3 of 9

Anthony Hopf wrote:
> I know there are functions like rot90 to rotate a matrix about a point,
> but you need the the matrix. I only have the linear index values of the
> matrix. I can easily shift the indexes by adding or subtracting either
> row or column values... but what if I wanted to rotate by 90 or 180
> degrees... is this possible with just the index values?

> and get
>
> A_shift = [55 44 33 22 11; 1010 99 88 77 66] without ever seeing A...
> but knowing its size and dimensions.

Yes, if you know the dimensions then you can calculate it.

[orig_row, orig_col] = ind2sub([A_rows, A_cols], A);
new_row_r90 = A_cols + 1 - orig_col;
new_col_r90 = orig_row;
A_shift = sub2ind([A_cols, A_row], new_row_r90, new_col_r90);

If you really wanted to, you could combine this all into a single formula.

Subject: index manipulation... shifting and rotating

From: Anthony Hopf

Date: 25 May, 2010 09:58:04

Message: 4 of 9

TideMan <mulgor@gmail.com> wrote in message <6fc95e16-178c-4bc0-b4f4-ee7e5fe58d93@x27g2000prf.googlegroups.com>...
> On May 25, 1:54 pm, "Anthony Hopf" <anthony.h...@gmail.com> wrote:
> > I know there are functions like rot90 to rotate a matrix about a point, but you need the the matrix.  I only have the linear index values of the matrix.  I can easily shift the indexes by adding or subtracting either row or column values... but what if I wanted to rotate by 90 or 180 degrees... is this possible with just the index values?
> >
> > -one idea I had was to progressively subtract (or add depending on which way I want to shift) more from each row index, but I don't have them in a specific order to implement this progressive shift.
> >
> > here is a small example:
> >
> > A = [ 11 22 33 44 55; 66 77 88 99 1010]
> >
> > idx(1,1) = [1 6 ]
> > idx(1,2) = [2 7 ]
> > idx(1,3) = [3 8]
> > idx(1,4) = [4 9]
> > idx(1,5) = [5 10]
> >
> > and get
> >
> > A_shift = [55 44 33 22 11; 1010 99 88 77 66] without ever seeing A... but knowing its size and dimensions.
> >
> > confusing?  thank you for the help... I know I've been asking quite a few questions lately but I am learning a lot!!
>
> I'm afraid your example makes no sense to me at all.
> For example, what does:
> idx(1,1)=[1 6]
> mean?
> There is one slot on the LHS, but 2 slots on the RHS, so that can't
> work.
>
> But to get A_shift from A, you don't need to screw around like that,
> just do:
> A_shift=fliplr(A);

Sorry for the confusion. The example of matrix idx should be (1,:)...(5,:) where each idx row is a vector of index values from A. I'm pretty sure Walter has given the solution...

Thank you

Anthony

Subject: index manipulation... shifting and rotating

From: Anthony Hopf

Date: 25 May, 2010 10:03:09

Message: 5 of 9

Walter Roberson <roberson@hushmail.com> wrote in message <nvGKn.18764$Gx2.3351@newsfe20.iad>...
> Anthony Hopf wrote:
>
> > and get
> >
> > A_shift = [55 44 33 22 11; 1010 99 88 77 66] without ever seeing A...
> > but knowing its size and dimensions.
>
> Yes, if you know the dimensions then you can calculate it.
>
> [orig_row, orig_col] = ind2sub([A_rows, A_cols], A);
> new_row_r90 = A_cols + 1 - orig_col;
> new_col_r90 = orig_row;
> A_shift = sub2ind([A_cols, A_row], new_row_r90, new_col_r90);
>
> If you really wanted to, you could combine this all into a single formula.

Walter,

Thank you, I'll play with this sample code... this is pretty tricky and a nice use of ind2sub and back that I would never have guessed, obviously. It looks like doing the other 90 deg rotation should be very easy too by subtracting from the "new_col_r90" matrix.

Thanks again!!

Anthony

Subject: index manipulation... shifting and rotating

From: Anthony Hopf

Date: 25 May, 2010 11:06:04

Message: 6 of 9

Walter Roberson <roberson@hushmail.com> wrote in message <nvGKn.18764$Gx2.3351@newsfe20.iad>...
> Anthony Hopf wrote:
> > I know there are functions like rot90 to rotate a matrix about a point,
> > but you need the the matrix. I only have the linear index values of the
> > matrix. I can easily shift the indexes by adding or subtracting either
> > row or column values... but what if I wanted to rotate by 90 or 180
> > degrees... is this possible with just the index values?
>
> > and get
> >
> > A_shift = [55 44 33 22 11; 1010 99 88 77 66] without ever seeing A...
> > but knowing its size and dimensions.
>
> Yes, if you know the dimensions then you can calculate it.
>
> [orig_row, orig_col] = ind2sub([A_rows, A_cols], A);
> new_row_r90 = A_cols + 1 - orig_col;
> new_col_r90 = orig_row;
> A_shift = sub2ind([A_cols, A_row], new_row_r90, new_col_r90);
>
> If you really wanted to, you could combine this all into a single formula.

Walter,

I'm having some trouble with the code. first off the matrix you use for A is actually the index matrix right? Also for my specific application I know the index values index into a 3d matrix indexing Cartesian space, NxNxL matrix. So I rewrote your code like this:

 [orig_row, orig_col] = ind2sub(size(XYZspace, idx_matrix);
 new_row_r90 = size(XYZspace,2) + 1 - orig_col;
 new_col_r90 = orig_row;
 idx_matrix_shift = sub2ind(size(XYZspace), new_row_r90, new_col_r90);

I use size(XYZspace) in both spots where before there was the matrix [A_rows, A_cols] and [A_cols, A_row], I can do this because the column and rows are the same, right?

The problem I run into is in the final step I get an error:
>>??? Error using ==> sub2ind at 58
>>Out of range subscript.

Am I missing something? I know that idx_matrix is a WxP matrix which is a composite of row vectors holding the index values... but some of the values are NaNs, could this be creating the error?

Thank you very much for all your help!!

Anthony

Subject: index manipulation... shifting and rotating

From: Walter Roberson

Date: 25 May, 2010 14:16:35

Message: 7 of 9

Anthony Hopf wrote:
> Walter Roberson <roberson@hushmail.com> wrote in message

>> [orig_row, orig_col] = ind2sub([A_rows, A_cols], A);

 > So I rewrote your code like this:

> [orig_row, orig_col] = ind2sub(size(XYZspace, idx_matrix);

You are missing a ) in that line.

> new_row_r90 = size(XYZspace,2) + 1 - orig_col;
> new_col_r90 = orig_row;
> idx_matrix_shift = sub2ind(size(XYZspace), new_row_r90, new_col_r90);
>
> I use size(XYZspace) in both spots where before there was the matrix
> [A_rows, A_cols] and [A_cols, A_row], I can do this because the column
> and rows are the same, right?

You need to explicitly work with the third index for a 3D matrix. When
you provide fewer indices than the dimension of the matrix, it is
probably going to try to use linear indexing.

Subject: index manipulation... shifting and rotating

From: Anthony Hopf

Date: 25 May, 2010 22:02:08

Message: 8 of 9

Walter Roberson <roberson@hushmail.com> wrote in message <8ZQKn.10564$%u7.1542@newsfe14.iad>...
> Anthony Hopf wrote:
> > Walter Roberson <roberson@hushmail.com> wrote in message
>
> >> [orig_row, orig_col] = ind2sub([A_rows, A_cols], A);
>
> > So I rewrote your code like this:
>
> > [orig_row, orig_col] = ind2sub(size(XYZspace, idx_matrix);
>
> You are missing a ) in that line.
>
> > new_row_r90 = size(XYZspace,2) + 1 - orig_col;
> > new_col_r90 = orig_row;
> > idx_matrix_shift = sub2ind(size(XYZspace), new_row_r90, new_col_r90);
> >
> > I use size(XYZspace) in both spots where before there was the matrix
> > [A_rows, A_cols] and [A_cols, A_row], I can do this because the column
> > and rows are the same, right?
>
> You need to explicitly work with the third index for a 3D matrix. When
> you provide fewer indices than the dimension of the matrix, it is
> probably going to try to use linear indexing.

Walter, sorry to bother you again, but could you please help clarify something for me?

I must have messed up when pasting (about the parenthesis)... but I went back with a 3d matrix and was hoping it would be as easy as adding the 3rd dimension to make it work... but I get the subscript out of range error again. I wrote this hoping that I would get a mirror image of the index values (similar to what fliplr would do):

[orig_row, orig_col, orig_3rd] = ind2sub(size(XYZspace), idx_matrix);
new_row_flip = orig_row;
new_col_flip = size(XYZspace,2) + 1 - orig_col;
new_3rd_flip = orig_3rd;
idx_matrix_flip = sub2ind(size(XYZspace), new_row_flip, new_col_flip,new_3rd_flip);

Am I using sub2ind incorrectly or is my understanding of your code incorrect?

Thanks again,

Anthony

Subject: index manipulation... shifting and rotating

From: Walter Roberson

Date: 25 May, 2010 22:33:18

Message: 9 of 9

Anthony Hopf wrote:

> I must have messed up when pasting (about the parenthesis)... but I went
> back with a 3d matrix and was hoping it would be as easy as adding the
> 3rd dimension to make it work... but I get the subscript out of range
> error again. I wrote this hoping that I would get a mirror image of the
> index values (similar to what fliplr would do):
>
> [orig_row, orig_col, orig_3rd] = ind2sub(size(XYZspace), idx_matrix);
> new_row_flip = orig_row;
> new_col_flip = size(XYZspace,2) + 1 - orig_col;
> new_3rd_flip = orig_3rd;
> idx_matrix_flip = sub2ind(size(XYZspace), new_row_flip,
> new_col_flip,new_3rd_flip);
>
> Am I using sub2ind incorrectly or is my understanding of your code
> incorrect?

Your code, above, works fine for me with XYZspace = rand(5,7,9) and
idx_matrix = find(XYZspace>0.9)

idx_matrix_flip will be (nc+1-2*b)*nr where nc is size(XYZspace,2) and nr is
size(XYZspace,1)

Tags for this Thread

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.

Contact us