http://www.mathworks.com/matlabcentral/newsreader/view_thread/306382
MATLAB Central Newsreader  Access a matrix rowwise without transpose?
Feed for thread: Access a matrix rowwise without transpose?
enus
©19942015 by MathWorks, Inc.
webmaster@mathworks.com
MATLAB Central Newsreader
http://blogs.law.harvard.edu/tech/rss
60
MathWorks
http://www.mathworks.com/images/membrane_icon.gif

Sun, 17 Apr 2011 04:06:04 +0000
Access a matrix rowwise without transpose?
http://www.mathworks.com/matlabcentral/newsreader/view_thread/306382#831519
Donald
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:<br>
<br>
aa = [1 1 2 2 3 3]; bb = [1 5 2 3 1 3];<br>
<br>
I create a matrix xx = zeros(5,3) and say xx( bb + (aa1)*5) = 1;<br>
<br>
now I have<br>
<br>
xx =<br>
<br>
1 0 1<br>
0 1 0<br>
0 1 1<br>
0 0 0<br>
1 0 0<br>
<br>
<br>
What I want to do now is read the values out of xx rowwise so that the elements in bb are now sorted. <br>
<br>
<br>
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.

Sun, 17 Apr 2011 05:04:10 +0000
Re: Access a matrix rowwise without transpose?
http://www.mathworks.com/matlabcentral/newsreader/view_thread/306382#831521
ImageAnalyst
Donald:<br>
I've read this over and over again (really hoping to help you) and I<br>
wonder if I'm the only one who can't understand it. What does this<br>
mean "I want to have vectors sorted by bb in ascending order"? What<br>
vectors are you talking about? "aa" is one vector I guess but what are<br>
the other vectors? And what does it mean to have those other vectors<br>
"sorted by bb in ascending order"? You create some 2D array called<br>
xx, and then calculate linear indices using the formula bb +<br>
(aa1)*5. I have no idea where that formula came from. Okay, maybe<br>
there's some reason for it (that was not explained), but then you set<br>
those linear indices to 1 and end up with some strange, more or less<br>
random arrangement of 1's in the xx array. If you want to get the<br>
rows out of xx just do this<br>
oneRow = xx(desiredRowNumber, :);<br>
But how does retrieving the rows of a transposed xx, and then using<br>
find() on that, now somehow magically make the bb vector sorted? And<br>
why is bb sorted anyway, since you said it's "aa" and other,<br>
unspecified vectors that are to be sorted by bb? Surely you don't<br>
sort bb by using itself to sort in ascending order. In other words,<br>
you don't have bb "sorted by bb in ascending order" do you? That<br>
would be some kind of weird recursive sort.<br>
<br>
I have no clue what you want. It's probably just me though. I'm sure<br>
to others, particularly those with the Crystal Ball Toolbox, it's<br>
crystal clear, and they will come up with a concise oneliner to solve<br>
your problem.<br>
<br>
Say, you aren't one of those guys who makes up derivatives for the<br>
financial industry are you?<br>
<br>
Sorry I'm of no help now  I guess I'm just saying I (and perhaps a<br>
few others) would need a different explanation if I (we) were to be<br>
able to help.<br>
<br>
Anyway, it sounds like you have a solution, it's just not as compact<br>
as you'd like. But I wouldn't worry about it. Hey, it works, and<br>
your arrays are so minuscule that it will take virtually no time at<br>
all so I'd just go with it.

Sun, 17 Apr 2011 05:20:24 +0000
Re: Access a matrix rowwise without transpose?
http://www.mathworks.com/matlabcentral/newsreader/view_thread/306382#831522
Nasser M. Abbasi
On 4/16/2011 9:06 PM, Donald wrote:<br>
> I'm trying to figure out if what I'm attempting to do is possible.<br>
> Suppose I have two vectors aa and bb. Suppose aa is sorted in ascending order<br>
> and I want to have vectors sorted by bb in ascending order.<br>
> I was thinking that at the expense of memory space I could do this by using a matrix. Here's my example:<br>
><br>
> aa = [1 1 2 2 3 3]; bb = [1 5 2 3 1 3];<br>
><br>
> I create a matrix xx = zeros(5,3) and say xx( bb + (aa1)*5) = 1;<br>
><br>
> now I have<br>
><br>
> xx =<br>
><br>
> 1 0 1<br>
> 0 1 0<br>
> 0 1 1<br>
> 0 0 0<br>
> 1 0 0<br>
><br>
><br>
> What I want to do now is read the values out of xx rowwise so that the elements in bb are now sorted.<br>
><br>
><br>
> If I transpose xx and then do a find, I can get what I want but I guess I'm looking<br>
> for a way to do it directly using xx. Maybe what I'm trying to do is impossible but it seems<br>
> like it should be possible.<br>
<br>
<br>
How about sorting xx in reverse order by transposing bb first<br>
where xx was taking as xx( bb + (aa1)*5)' = 1 but reading<br>
xx column wise instead of rowwise so that aa is sorted<br>
in descending order?<br>
<br>
Nasser

Sun, 17 Apr 2011 05:38:04 +0000
Re: Access a matrix rowwise without transpose?
http://www.mathworks.com/matlabcentral/newsreader/view_thread/306382#831523
Roger Stafford
"Donald " <gt4715b@yahoo.com> wrote in message <iodovc$3b3$1@fred.mathworks.com>...<br>
> 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:<br>
> <br>
> aa = [1 1 2 2 3 3]; bb = [1 5 2 3 1 3];<br>
> <br>
> I create a matrix xx = zeros(5,3) and say xx( bb + (aa1)*5) = 1;<br>
> <br>
> now I have<br>
> <br>
> xx =<br>
> <br>
> 1 0 1<br>
> 0 1 0<br>
> 0 1 1<br>
> 0 0 0<br>
> 1 0 0<br>
> <br>
> What I want to do now is read the values out of xx rowwise so that the elements in bb are now sorted. <br>
> <br>
> 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.<br>
         <br>
Well, you can use this method:<br>
<br>
p = 0:14;<br>
t = find(xx(mod(5*p,15)+floor(p/3)+1)~=0);<br>
<br>
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.<br>
<br>
Roger Stafford

Sun, 17 Apr 2011 05:54:01 +0000
Re: Access a matrix rowwise without transpose?
http://www.mathworks.com/matlabcentral/newsreader/view_thread/306382#831526
Nasser M. Abbasi
On 4/16/2011 10:38 PM, Roger Stafford wrote:<br>
<br>
>          <br>
> Well, you can use this method:<br>
><br>
> p = 0:14;<br>
> t = find(xx(mod(5*p,15)+floor(p/3)+1)~=0);<br>
><br>
> The generalization to any size xx is obvious. This gives you sequential linear indices in<br>
> row order in xx. Now you have to run these back through ind2sub to get the sorted bb.<br>
> It seems an awfully cumbersome way of sorting.<br>
><br>
> Roger Stafford<br>
<br>
Roger,<br>
<br>
If the above is actual solution, and you actually understand the question,<br>
then all what I have to say is that you are a true Genius.<br>
<br>
My solution was just kidding around, becuase I had no clue at all what<br>
the question was saying :)<br>
<br>
Nasser

Sun, 17 Apr 2011 08:30:20 +0000
Re: Access a matrix rowwise without transpose?
http://www.mathworks.com/matlabcentral/newsreader/view_thread/306382#831532
Roger Stafford
"Nasser M. Abbasi" <nma@12000.org> wrote in message <iodva4$4hp$1@speranza.aioe.org>...<br>
> Roger,<br>
> <br>
> If the above is actual solution, and you actually understand the question,<br>
> then all what I have to say is that you are a true Genius.<br>
> <br>
> My solution was just kidding around, becuase I had no clue at all what<br>
> the question was saying :)<br>
> <br>
> Nasser<br>
      <br>
The only part I really understood well, Nasser, was how to traverse 'xx' rowwise 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.<br>
<br>
Roger Stafford

Sun, 17 Apr 2011 12:49:18 +0000
Re: Access a matrix rowwise without transpose?
http://www.mathworks.com/matlabcentral/newsreader/view_thread/306382#831550
ImageAnalyst
On Apr 17, 1:54 am, "Nasser M. Abbasi" <n...@12000.org> wrote:<br>
> If the above is actual solution, and you actually understand the question,<br>
> then all what I have to say is that you are a true Genius.<br>
> Nasser<br>
<br>
He's got it. He's finally got it. Roger has the beta version of the<br>
Crystal Ball Toolbox. That lucky son of a gun!

Sun, 17 Apr 2011 13:30:19 +0000
Re: Access a matrix rowwise without transpose?
http://www.mathworks.com/matlabcentral/newsreader/view_thread/306382#831559
Donald
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. <br>
<br>
% row(bb) and column(aa) indices into an array<br>
>>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]<br>
<br>
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<br>
<br>
>>x = zeros(M,N); x(bb + (aa1)*M) = 1;<br>
<br>
xx =<br>
<br>
1 0 1<br>
0 1 0<br>
0 1 1<br>
0 0 0<br>
1 0 0<br>
<br>
<br>
Now, I want to find the row indices for the nonzero elements read rowwise from the array. Thus, bb becomes sorted<br>
<br>
The way I have of doing it right now is <br>
<br>
[bb2,aa2] = find( xx')<br>
<br>
bb2 = [1 3 2 2 3 1]; aa2 = [1 1 2 3 3 5]; <br>
<br>
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.

Sun, 17 Apr 2011 14:19:24 +0000
Re: Access a matrix rowwise without transpose?
http://www.mathworks.com/matlabcentral/newsreader/view_thread/306382#831567
ImageAnalyst
Why not just ditch xx and do it directly:<br>
<br>
aa = [1 1 2 2 3 3];<br>
bb = [1 5 2 3 1 3];<br>
<br>
[aa2 sortIndices] = sort(bb)<br>
bb22 = aa(sortIndices)<br>
<br>
Results:<br>
<br>
aa2 =<br>
1 1 2 3 3 5<br>
sortIndices =<br>
1 5 3 4 6 2<br>
bb22 =<br>
1 3 2 2 3 1