Go Back   Rhinocerus > Newsgroup > Newsgroup comp.lang.* 2 > Newsgroup comp.lang.apl

Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old 06-13-2012, 03:47 PM
Lance
Guest
 
Posts: n/a
Default indexing a matrix

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
Reply With Quote
Alt Today
Advertising
 
and become member of Rhinocerus
Standard Sponsored Links

  #2 (permalink)  
Old 06-13-2012, 03:48 PM
Lance
Guest
 
Posts: n/a
Default Re: indexing a matrix

On Jun 13, 10:47*am, Lance <lanc...@gmail.com> 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.
>
> 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


Please excuse the poor formatting. It looked fine before I submitted.
Reply With Quote
  #3 (permalink)  
Old 06-13-2012, 03:52 PM
Lance
Guest
 
Posts: n/a
Default Re: indexing a matrix

Hopefully this formats better:

for example, if MATRIX
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

Reply With Quote
  #4 (permalink)  
Old 06-13-2012, 03:58 PM
Bob Smith
Guest
 
Posts: n/a
Default Re: indexing a matrix

On 6/13/2012 11:47 AM, 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.
>
> 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?


⊃(⊂¨⊂[2]INDEX)⌷¨⊂[2]MATRIX

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

To reply to me directly, delete "despam".
Reply With Quote
  #5 (permalink)  
Old 06-13-2012, 04:11 PM
Lance
Guest
 
Posts: n/a
Default Re: indexing a matrix

>
> ⊃(⊂¨⊂[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?
Reply With Quote
  #6 (permalink)  
Old 06-13-2012, 05:11 PM
Bob Smith
Guest
 
Posts: n/a
Default Re: indexing a matrix

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 -- bsmith@sudleydeplacespam.com

To reply to me directly, delete "despam".
Reply With Quote
  #7 (permalink)  
Old 06-14-2012, 02:48 AM
Ric
Guest
 
Posts: n/a
Default Re: indexing a matrix

On Thursday, 14 June 2012 03:52:17 UTC+12, Lance wrote:
> Hopefully this formats better:
>
> for example, if MATRIX
> 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


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
3 2 1
5 4 6
7 8 9
11 12 10
Reply With Quote
  #8 (permalink)  
Old 06-14-2012, 11:16 PM
Aaron W. Hsu
Guest
 
Posts: n/a
Default Re: indexing a matrix

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 | arcfide@sacrideo.us | http://www.sacrideo.us
Programming is just another word for the lost art of thinking.
Reply With Quote
  #9 (permalink)  
Old 06-15-2012, 09:17 PM
Gary Logan
Guest
 
Posts: n/a
Default Re: indexing a matrix

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.
Reply With Quote
  #10 (permalink)  
Old 06-15-2012, 10:19 PM
Aaron W. Hsu
Guest
 
Posts: n/a
Default Re: indexing a matrix

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 | arcfide@sacrideo.us | http://www.sacrideo.us
Programming is just another word for the lost art of thinking.
Reply With Quote
  #11 (permalink)  
Old 07-02-2012, 10:02 PM
Graham
Guest
 
Posts: n/a
Default Re: indexing a matrix


"Lance" <lancemd@gmail.com> wrote in message news:8e5359cc-af72-4a7d-9943-aac2d1f55e12@b1g2000vbb.googlegroups.com...
> Hopefully this formats better:
>
> for example, if MATRIX
> 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


Try this:

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

Graham.
Reply With Quote
  #12 (permalink)  
Old 07-05-2012, 05:22 AM
Gary Logan
Guest
 
Posts: n/a
Default Re: indexing a matrix

On 7/2/2012 4:02 PM, Graham wrote:
> "Lance" <lancemd@gmail.com> wrote in message news:8e5359cc-af72-4a7d-9943-aac2d1f55e12@b1g2000vbb.googlegroups.com...
>> Hopefully this formats better:
>>
>> for example, if MATRIX
>> 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

> Try this:
>
> (⍴MATRIX)⍴(,MATRIX)[(,INDEX)+n×(n←¯1↑⍴INDEX)/¯1+⍳↑⍴INDEX]
>
> Graham.


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.


Reply With Quote
  #13 (permalink)  
Old 07-05-2012, 08:38 AM
Graham
Guest
 
Posts: n/a
Default Re: indexing a matrix


"Gary Logan" <glogan1513@gmail.com> wrote in message news:jt38av$k9v$1@dont-email.me...
> On 7/2/2012 4:02 PM, Graham wrote:
>> "Lance" <lancemd@gmail.com> wrote in message news:8e5359cc-af72-4a7d-9943-aac2d1f55e12@b1g2000vbb.googlegroups.com...
>>> Hopefully this formats better:
>>>
>>> for example, if MATRIX
>>> 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

>> Try this:
>>
>> (⍴MATRIX)⍴(,MATRIX)[(,INDEX)+n×(n←¯1↑⍴INDEX)/¯1+⍳↑⍴INDEX]
>>
>> Graham.

>
> 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 /.


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.
Reply With Quote
  #14 (permalink)  
Old 07-31-2012, 01:55 PM
ArrayMac
Guest
 
Posts: n/a
Default Re: indexing a matrix

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.
Reply With Quote
 
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




All times are GMT. The time now is 04:09 AM.


Copyright ©2009

LinkBacks Enabled by vBSEO 3.3.0 RC2 © 2009, Crawlability, Inc.