From: Anthony Hopf on
Walter Roberson <roberson(a)hushmail.com> wrote in message <nvGKn.18764$Gx2.3351(a)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
From: Walter Roberson on
Anthony Hopf wrote:
> Walter Roberson <roberson(a)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.
From: Anthony Hopf on
Walter Roberson <roberson(a)hushmail.com> wrote in message <8ZQKn.10564$%u7.1542(a)newsfe14.iad>...
> Anthony Hopf wrote:
> > Walter Roberson <roberson(a)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
From: Walter Roberson on
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)