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:
Access a matrix row-wise without transpose?

Subject: Access a matrix row-wise without transpose?

From: Donald

Date: 17 Apr, 2011 04:06:04

Message: 1 of 9

I'm trying to figure out if what I'm attempting to do is possible. Suppose I have two vectors aa and bb. Suppose aa is sorted in ascending order and I want to have vectors sorted by bb in ascending order. I was thinking that at the expense of memory space I could do this by using a matrix. Here's my example:

aa = [1 1 2 2 3 3]; bb = [1 5 2 3 1 3];

I create a matrix xx = zeros(5,3) and say xx( bb + (aa-1)*5) = 1;

now I have

xx =

     1 0 1
     0 1 0
     0 1 1
     0 0 0
     1 0 0


What I want to do now is read the values out of xx rowwise so that the elements in bb are now sorted.


 If I transpose xx and then do a find, I can get what I want but I guess I'm looking for a way to do it directly using xx. Maybe what I'm trying to do is impossible but it seems like it should be possible.

Subject: Access a matrix row-wise without transpose?

From: ImageAnalyst

Date: 17 Apr, 2011 05:04:10

Message: 2 of 9

Donald:
I've read this over and over again (really hoping to help you) and I
wonder if I'm the only one who can't understand it. What does this
mean "I want to have vectors sorted by bb in ascending order"? What
vectors are you talking about? "aa" is one vector I guess but what are
the other vectors? And what does it mean to have those other vectors
"sorted by bb in ascending order"? You create some 2D array called
xx, and then calculate linear indices using the formula bb +
(aa-1)*5. I have no idea where that formula came from. Okay, maybe
there's some reason for it (that was not explained), but then you set
those linear indices to 1 and end up with some strange, more or less
random arrangement of 1's in the xx array. If you want to get the
rows out of xx just do this
oneRow = xx(desiredRowNumber, :);
But how does retrieving the rows of a transposed xx, and then using
find() on that, now somehow magically make the bb vector sorted? And
why is bb sorted anyway, since you said it's "aa" and other,
unspecified vectors that are to be sorted by bb? Surely you don't
sort bb by using itself to sort in ascending order. In other words,
you don't have bb "sorted by bb in ascending order" do you? That
would be some kind of weird recursive sort.

I have no clue what you want. It's probably just me though. I'm sure
to others, particularly those with the Crystal Ball Toolbox, it's
crystal clear, and they will come up with a concise one-liner to solve
your problem.

Say, you aren't one of those guys who makes up derivatives for the
financial industry are you?

Sorry I'm of no help now -- I guess I'm just saying I (and perhaps a
few others) would need a different explanation if I (we) were to be
able to help.

Anyway, it sounds like you have a solution, it's just not as compact
as you'd like. But I wouldn't worry about it. Hey, it works, and
your arrays are so minuscule that it will take virtually no time at
all so I'd just go with it.

Subject: Access a matrix row-wise without transpose?

From: Nasser M. Abbasi

Date: 17 Apr, 2011 05:20:24

Message: 3 of 9

On 4/16/2011 9:06 PM, Donald wrote:
> I'm trying to figure out if what I'm attempting to do is possible.
> Suppose I have two vectors aa and bb. Suppose aa is sorted in ascending order
> and I want to have vectors sorted by bb in ascending order.
> I was thinking that at the expense of memory space I could do this by using a matrix. Here's my example:
>
> aa = [1 1 2 2 3 3]; bb = [1 5 2 3 1 3];
>
> I create a matrix xx = zeros(5,3) and say xx( bb + (aa-1)*5) = 1;
>
> now I have
>
> xx =
>
> 1 0 1
> 0 1 0
> 0 1 1
> 0 0 0
> 1 0 0
>
>
> What I want to do now is read the values out of xx rowwise so that the elements in bb are now sorted.
>
>
> If I transpose xx and then do a find, I can get what I want but I guess I'm looking
> for a way to do it directly using xx. Maybe what I'm trying to do is impossible but it seems
> like it should be possible.


How about sorting xx in reverse order by transposing bb first
where xx was taking as xx( bb + (aa-1)*5)' = 1 but reading
xx column wise instead of rowwise so that aa is sorted
in descending order?

--Nasser

Subject: Access a matrix row-wise without transpose?

From: Roger Stafford

Date: 17 Apr, 2011 05:38:04

Message: 4 of 9

"Donald " <gt4715b@yahoo.com> wrote in message <iodovc$3b3$1@fred.mathworks.com>...
> I'm trying to figure out if what I'm attempting to do is possible. Suppose I have two vectors aa and bb. Suppose aa is sorted in ascending order and I want to have vectors sorted by bb in ascending order. I was thinking that at the expense of memory space I could do this by using a matrix. Here's my example:
>
> aa = [1 1 2 2 3 3]; bb = [1 5 2 3 1 3];
>
> I create a matrix xx = zeros(5,3) and say xx( bb + (aa-1)*5) = 1;
>
> now I have
>
> xx =
>
> 1 0 1
> 0 1 0
> 0 1 1
> 0 0 0
> 1 0 0
>
> What I want to do now is read the values out of xx rowwise so that the elements in bb are now sorted.
>
> If I transpose xx and then do a find, I can get what I want but I guess I'm looking for a way to do it directly using xx. Maybe what I'm trying to do is impossible but it seems like it should be possible.
- - - - - - - - - -
  Well, you can use this method:

 p = 0:14;
 t = find(xx(mod(5*p,15)+floor(p/3)+1)~=0);

The generalization to any size xx is obvious. This gives you sequential linear indices in row order in xx. Now you have to run these back through ind2sub to get the sorted bb. It seems an awfully cumbersome way of sorting.

Roger Stafford

Subject: Access a matrix row-wise without transpose?

From: Nasser M. Abbasi

Date: 17 Apr, 2011 05:54:01

Message: 5 of 9

On 4/16/2011 10:38 PM, Roger Stafford wrote:

> - - - - - - - - - -
> Well, you can use this method:
>
> p = 0:14;
> t = find(xx(mod(5*p,15)+floor(p/3)+1)~=0);
>
> The generalization to any size xx is obvious. This gives you sequential linear indices in
> row order in xx. Now you have to run these back through ind2sub to get the sorted bb.
> It seems an awfully cumbersome way of sorting.
>
> Roger Stafford

Roger,

If the above is actual solution, and you actually understand the question,
then all what I have to say is that you are a true Genius.

My solution was just kidding around, becuase I had no clue at all what
the question was saying :)

--Nasser

Subject: Access a matrix row-wise without transpose?

From: Roger Stafford

Date: 17 Apr, 2011 08:30:20

Message: 6 of 9

"Nasser M. Abbasi" <nma@12000.org> wrote in message <iodva4$4hp$1@speranza.aioe.org>...
> Roger,
>
> If the above is actual solution, and you actually understand the question,
> then all what I have to say is that you are a true Genius.
>
> My solution was just kidding around, becuase I had no clue at all what
> the question was saying :)
>
> --Nasser
- - - - - - -
  The only part I really understood well, Nasser, was how to traverse 'xx' row-wise using linear indexing. The rest appears to me to be "awfully cumbersome" as I stated before. It seems to me that he is, in effect, tossing 'bb' values into as many matching numbered compartments as there are different values in 'bb' and then taking them out starting from the smallest valued compartment on up, thereby sorting them. One thing is very clear. His method is very restricted in the kind of vectors that could be sorted by this procedure.

Roger Stafford

Subject: Access a matrix row-wise without transpose?

From: ImageAnalyst

Date: 17 Apr, 2011 12:49:18

Message: 7 of 9

On Apr 17, 1:54 am, "Nasser M. Abbasi" <n...@12000.org> wrote:
> If the above is actual solution, and you actually understand the question,
> then all what I have to say is that you are a true Genius.
> --Nasser
--------------------------------------------------------------------------------------
He's got it. He's finally got it. Roger has the beta version of the
Crystal Ball Toolbox. That lucky son of a gun!

Subject: Access a matrix row-wise without transpose?

From: Donald

Date: 17 Apr, 2011 13:30:19

Message: 8 of 9

Thanks for the help guys. I guess I didn't explain myself very well. Indeed, what I'm trying to start with two vectors that are the row and column indices into an array where one is sorted, and end up with the indices sorted the other way. Here is my example again.

% row(bb) and column(aa) indices into an array
>>aa = [1 1 2 2 3 3]; bb = [1 5 2 3 1 3]; where aa lies in [1,N] and bb lies in [1,M]

Note that aa and bb are not really indices into an array, but I am attempting to use an array to sort. Anyway, I create an array x = zeros(M,N) and seed the array with the indices of aa and bb

>>x = zeros(M,N); x(bb + (aa-1)*M) = 1;

xx =

     1 0 1
     0 1 0
     0 1 1
     0 0 0
     1 0 0


Now, I want to find the row indices for the non-zero elements read rowwise from the array. Thus, bb becomes sorted

The way I have of doing it right now is

 [bb2,aa2] = find( xx')

bb2 = [1 3 2 2 3 1]; aa2 = [1 1 2 3 3 5];

This method is indeed cumbersome for sorting bb. What I really need is a direct way to sort bb, just using bb,aa,M and N. I just can't think of it.

Subject: Access a matrix row-wise without transpose?

From: ImageAnalyst

Date: 17 Apr, 2011 14:19:24

Message: 9 of 9

Why not just ditch xx and do it directly:

aa = [1 1 2 2 3 3];
bb = [1 5 2 3 1 3];

[aa2 sortIndices] = sort(bb)
bb22 = aa(sortIndices)

Results:

aa2 =
     1 1 2 3 3 5
sortIndices =
     1 5 3 4 6 2
bb22 =
     1 3 2 2 3 1

Tags for this Thread

No tags are associated with 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