Finding an Element in a Rank 2 Array and Assignment

Learning APL or new to Dyalog? Ask "silly" questions here, without fear...

Finding an Element in a Rank 2 Array and Assignment

Consider the following problem. The rank 2 array Mat (all numerical data) exists and it is required to find all of the indexes [row;column] of elements that have the value 6. After finding the indexes corresponding to the element value 6, change all of those element values to 60. For example:

Mat←3 3⍴⍳9
Mat
1 2 3
4 5 6
7 8 9
Mat=6
0 0 0
0 0 1
0 0 0

How do I find the indexes [row;column] corresponding to the element value 6 and change that element value to 60?

Thanks for any suggestions.
REMINGTON30

Posts: 19
Joined: Fri Aug 11, 2017 2:17 pm

Re: Finding an Element in a Rank 2 Array and Assignment

Code: Select all
`To make changes:  ((,6=Mat)/,Mat)←60To find indices:  (,6=Mat)/,⍳⍴Mat`
Roger|Dyalog

Posts: 215
Joined: Thu Jul 28, 2011 10:53 am

Re: Finding an Element in a Rank 2 Array and Assignment

If you don't have to know the indices, the new At operator might be interesting, too:
`      60@(=∘6)⊢3 3⍴⍳9 1 2  34 5 607 8  9`

-Veli-Matti
Veli-Matti

Posts: 56
Joined: Sat Nov 28, 2009 3:12 pm

Re: Finding an Element in a Rank 2 Array and Assignment

Or using array logic:
`      (⊢×10*6=⊢)3 3⍴⍳9 0 1 2 3 4 560 7 8`

Phil Last

Posts: 557
Joined: Thu Jun 18, 2009 6:29 pm

Re: Finding an Element in a Rank 2 Array and Assignment

Thank you all for the very useful responses. I have not used the i⍴Mat expression before with Mat a rank 2 array. In the actual application, a rank 2 array is transferred to APL from Excel spreadsheet. The array is mostly numerical data but contains a few blank spaces for elements. The blank spaces must be replaced with zeros before processing.
REMINGTON30

Posts: 19
Joined: Fri Aug 11, 2017 2:17 pm

Re: Finding an Element in a Rank 2 Array and Assignment

If you like the ⍳⍴ method, you may also like the new ⍸ primitive "Where" which gives the list of indices of all 1a in a Boolean array:
`      ⎕←Mat←3 3⍴1 ' ' 3 4 5 ' ' ' ' 8 91   34 5    8 9      ⍸Mat=' '┌───┬───┬───┐│1 2│2 3│3 1│└───┴───┴───┘      Mat[⍸Mat=' ']←0      Mat1 0 34 5 00 8 9`

Posts: 88
Joined: Thu Jun 25, 2015 1:13 pm

Re: Finding an Element in a Rank 2 Array and Assignment

When I looked at this question a few days ago, I was going to suggest scatter point indexing, but generating the indices takes more effort than the other solutions. It never occurred to me to use the new where primitive, which obviously makes total sense!

Very, very nice.
paulmansour

Posts: 343
Joined: Fri Oct 03, 2008 4:14 pm

Re: Finding an Element in a Rank 2 Array and Assignment

This
`      Dyalog APL/W-64 Version 16.0.31693Serial No : XXXXXXUnicode EditionSun Sep  2 21:57:01 2018      z←2=?10 10⍴10      z0 0 0 0 0 0 0 0 0 00 1 0 1 0 0 0 1 0 00 0 0 1 0 0 0 0 0 00 1 0 0 0 0 0 0 0 00 0 0 0 0 0 0 1 0 00 0 1 0 0 0 0 0 0 00 0 0 0 0 0 1 0 0 00 0 1 0 0 0 0 0 0 00 0 0 0 0 0 0 1 0 00 0 0 0 0 0 0 1 0 0      ⍸z 1 1  1 3  1 7  2 3  3 1  4 7  5 2  6 6  7 2  8 7  9 7       {↓⍉(⍴⍵)⊤(,⍵)/⍳⍴,⍵}z  1 1  1 3  1 7  2 3  3 1  4 7  5 2  6 6  7 2  8 7  9 7       cmpx'⍸z' '{↓⍉(⍴⍵)⊤(,⍵)/⍳⍴,⍵}z'  ⍸z                  → 3.8E¯6 |   0% ⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕  {↓⍉(⍴⍵)⊤(,⍵)/⍳⍴,⍵}z → 1.3E¯6 | -66% ⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕`
Might well have improved in later versions.

Phil Last

Posts: 557
Joined: Thu Jun 18, 2009 6:29 pm

Re: Finding an Element in a Rank 2 Array and Assignment

Hi Phil,

The second expression (which needs ⎕IO←0) takes about 50% longer than ⍸ in version 17.0.

Try it online!

Posts: 88
Joined: Thu Jun 25, 2015 1:13 pm

Re: Finding an Element in a Rank 2 Array and Assignment

Again, thank you all for the responses.

Based on your comments, it is clear that the "where" function is ideally suited to finding indexes of array elements that meet specified criteria.

More generally, I find the ability to transfer data to and from Excel and do the data processing in APL to be very useful.

I have used APL for only about 14 months. The Dyalog documentation and support is excellent. Your comments on the Forum have been a very useful part of the learning process.
REMINGTON30

Posts: 19
Joined: Fri Aug 11, 2017 2:17 pm