Page 1 of 1
Finding an Element in a Rank 2 Array and Assignment
Posted: Tue Aug 28, 2018 6:04 pm
by REMINGTON30
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.
Re: Finding an Element in a Rank 2 Array and Assignment
Posted: Tue Aug 28, 2018 7:10 pm
by Roger|Dyalog
Code: Select all
To make changes: ((,6=Mat)/,Mat)←60
To find indices: (,6=Mat)/,⍳⍴Mat
Re: Finding an Element in a Rank 2 Array and Assignment
Posted: Wed Aug 29, 2018 5:40 am
by Veli-Matti
If you don't have to know the indices, the new At operator might be interesting, too:
60@(=∘6)⊢3 3⍴⍳9
1 2 3
4 5 60
7 8 9
-Veli-Matti
Re: Finding an Element in a Rank 2 Array and Assignment
Posted: Wed Aug 29, 2018 7:21 am
by Phil Last
Or using array logic:
(⊢×10*6=⊢)3 3⍴⍳9
0 1 2
3 4 5
60 7 8
Re: Finding an Element in a Rank 2 Array and Assignment
Posted: Wed Aug 29, 2018 6:28 pm
by REMINGTON30
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.
Re: Finding an Element in a Rank 2 Array and Assignment
Posted: Sun Sep 02, 2018 6:56 am
by Adam|Dyalog
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 9
1 3
4 5
8 9
⍸Mat=' '
┌───┬───┬───┐
│1 2│2 3│3 1│
└───┴───┴───┘
Mat[⍸Mat=' ']←0
Mat
1 0 3
4 5 0
0 8 9
Re: Finding an Element in a Rank 2 Array and Assignment
Posted: Sun Sep 02, 2018 8:16 pm
by paulmansour
Adam, very nice!
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.
Re: Finding an Element in a Rank 2 Array and Assignment
Posted: Sun Sep 02, 2018 10:01 pm
by Phil Last
This
Dyalog APL/W-64 Version 16.0.31693
Serial No : XXXXXX
Unicode Edition
Sun Sep 2 21:57:01 2018
z←2=?10 10⍴10
z
0 0 0 0 0 0 0 0 0 0
0 1 0 1 0 0 0 1 0 0
0 0 0 1 0 0 0 0 0 0
0 1 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1 0 0
0 0 1 0 0 0 0 0 0 0
0 0 0 0 0 0 1 0 0 0
0 0 1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1 0 0
0 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.
Re: Finding an Element in a Rank 2 Array and Assignment
Posted: Mon Sep 03, 2018 5:44 am
by Adam|Dyalog
Hi Phil,
The second expression (which needs ⎕IO←0) takes about 50% longer than ⍸ in version 17.0.
Try it online!
Re: Finding an Element in a Rank 2 Array and Assignment
Posted: Wed Sep 05, 2018 7:13 pm
by REMINGTON30
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.