|
|||
|
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 |
|
|
||||
|
||||
|
|
|
|||
|
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. |
|
|||
|
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". |
|
|||
|
>
> ⊃(⊂¨⊂[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? |
|
|||
|
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". |
|
|||
|
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 |
|
|||
|
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. |
|
|||
|
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. |
|
|||
|
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. |
|
|||
|
"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. |
|
|||
|
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. |
|
|||
|
"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. |
|
|||
|
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. |
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|