Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

indexing a matrix

186 views
Skip to first unread message

Lance

unread,
Jun 13, 2012, 11:47:13 AM6/13/12
to
I have a matrix MATRIX with values that I want to rearrange. I also
have another matrix INDEX that is the same shape of MATRIX that has
the order of the columns for the rearrangement.

for example, if MATRIX is 1 2 3
4 5 6
7 8 9
10 11 12

and INDEX is: 3 2 1
2 1 3
1 2 3
2 3 1

then my final matrix I want to have look like: 3 2 1
5 4
6
7 8
9
11 12 10

Is there any succinct way of creating this new matrix using MATRIX and
INDEX?

Lance

Lance

unread,
Jun 13, 2012, 11:52:17 AM6/13/12
to
Hopefully this formats better:

for example, if MATRIX

Bob Smith

unread,
Jun 13, 2012, 11:58:39 AM6/13/12
to
⊃(⊂¨⊂[2]INDEX)⌷¨⊂[2]MATRIX

--
_________________________________________
Bob Smith -- bsm...@sudleydeplacespam.com

To reply to me directly, delete "despam".
Message has been deleted

Lance

unread,
Jun 13, 2012, 12:11:48 PM6/13/12
to
>
> ⊃(⊂¨⊂[2]INDEX)⌷¨⊂[2]MATRIX
>
> --
> _________________________________________
> Bob Smith -- bsm...@sudleydeplacespam.com
>
Thanks for the reply. Unfortunately this isn't working for me. I'm
working with APL+Win 2.0. Could it be my version of APL?

Bob Smith

unread,
Jun 13, 2012, 1:11:46 PM6/13/12
to
On 6/13/2012 12:11 PM, Lance wrote:
>>
>> ⊃(⊂¨⊂[2]INDEX)⌷¨⊂[2]MATRIX
>>
> Thanks for the reply. Unfortunately this isn't working for me. I'm
> working with APL+Win 2.0. Could it be my version of APL?

That's a very old version -- apparently it doesn't have an indexing
function (⌷).

If it supports axis operator on scalar dyadics, try

(,MATRIX)[INDEX+[1](1↓⍴MATRIX)ׯ1+⍳1↑⍴MATRIX]

If not, try

(,MATRIX)[INDEX+⍉(⌽⍴INDEX)⍴(1↓⍴MATRIX)ׯ1+⍳1↑⍴MATRIX]

For a more modern APL, try NARS2000 -- it's free at nars200.org.

--
_________________________________________
Bob Smith -- bsm...@sudleydeplacespam.com

Ric

unread,
Jun 13, 2012, 10:48:45 PM6/13/12
to
The following shows (in J) how the concept of rank can be used to solve this problem. (needs INDEX-1 because J uses Index Origin 0).

(INDEX-1) {"1 MATRIX

Aaron W. Hsu

unread,
Jun 14, 2012, 7:16:53 PM6/14/12
to
On Wed, 13 Jun 2012 08:47:13 -0700, Lance wrote:

> I have a matrix MATRIX with values that I want to rearrange. I also
> have another matrix INDEX that is the same shape of MATRIX that has the
> order of the columns for the rearrangement.

This is what I would do in Dyalog:

M←4 3⍴⍳12
I←4 3⍴3 2 1 2 1 3 1 2 3 2 3 1
M[(⍉3 4⍴⍳4),¨I]
3 2 1
5 4 6
7 8 9
11 12 10



--
Aaron W. Hsu | arc...@sacrideo.us | http://www.sacrideo.us
Programming is just another word for the lost art of thinking.

Gary Logan

unread,
Jun 15, 2012, 5:17:57 PM6/15/12
to
On Jun 14, 5:16 pm, "Aaron W. Hsu" <arcf...@sacrideo.us> wrote:
> On Wed, 13 Jun 2012 08:47:13 -0700, Lance wrote:
> > I have a matrix MATRIX with values that I want to rearrange.  I also
> > have another matrix INDEX that is the same shape of MATRIX that has the
> > order of the columns for the rearrangement.
>
> This is what I would do in Dyalog:
>
>         M←4 3⍴⍳12
>         I←4 3⍴3 2 1 2 1 3 1 2 3 2 3 1
>         M[(⍉3 4⍴⍳4),¨I]
>  3  2  1
>  5  4  6
>  7  8  9
> 11 12 10
>
> --
> Aaron W. Hsu | arcf...@sacrideo.us |http://www.sacrideo.us
> Programming is just another word for the lost art of thinking.

That is nice. I wasn't aware of that form of indexing.
Glad I had the free copy of Dyalog to check it out.

Aaron W. Hsu

unread,
Jun 15, 2012, 6:19:27 PM6/15/12
to
On Fri, 15 Jun 2012 14:17:57 -0700, Gary Logan wrote:

> That is nice. I wasn't aware of that form of indexing.
> Glad I had the free copy of Dyalog to check it out.

Personally I would prefer a non-nested indexing solution like this, but
the nested solutions works well enough in practice, and can be remarkably
efficient, despite its nested nature.

--
Aaron W. Hsu | arc...@sacrideo.us | http://www.sacrideo.us

Graham

unread,
Jul 2, 2012, 6:02:13 PM7/2/12
to

"Lance" <lan...@gmail.com> wrote in message news:8e5359cc-af72-4a7d...@b1g2000vbb.googlegroups.com...
Try this:

(⍴MATRIX)⍴(,MATRIX)[(,INDEX)+n×(n←¯1↑⍴INDEX)/¯1+⍳↑⍴INDEX]

Graham.

Gary Logan

unread,
Jul 5, 2012, 1:22:44 AM7/5/12
to
That may work, I'm not familiar with APL+Win2.0, but
there are a couple of constructs which could cause an
older interpreter to burp. Namely, the use of ↑ for 1↑
and using an extended definition of /.

Bob Smith gave three solutions to the problem and his
third solution should work as far back as APL\360.


Graham

unread,
Jul 5, 2012, 4:38:19 AM7/5/12
to

"Gary Logan" <gloga...@gmail.com> wrote in message news:jt38av$k9v$1...@dont-email.me...
I have been a life long user of APL+WIN and I have tested it as far back as APL*PLUS III which was a precursor to the APL+WIN family and it works fine.

Graham.

ArrayMac

unread,
Jul 31, 2012, 9:55:35 AM7/31/12
to
On Wednesday, 13 June 2012 12:47:13 UTC-3, Lance wrote:
> I have a matrix MATRIX with values that I want to rearrange. I also
>
> have another matrix INDEX that is the same shape of MATRIX that has
>
> the order of the columns for the rearrangement.
...
> Is there any succinct way of creating this new matrix using MATRIX and
>
> INDEX?
>
> Lance

Dyalog APL, and 'AssertEq' does a check for equality....

MATRIX←4 3⍴⍳12
INDEX←4 3⍴3 2 1 2 1 3 1 2 3 2 3 1
RESULT←4 3 ⍴ 3 2 1 5 4 6 7 8 9 11 12 10
AssertEq RESULT on 4 3⍴MATRIX[↑(⍳4),¨¨↓INDEX]

I hope I matched your data correctly.
0 new messages