Finding an Element in a Rank 2 Array and Assignment
-
- Posts: 22
- Joined: Fri Aug 11, 2017 2:17 pm
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.
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.
-
- Posts: 238
- Joined: Thu Jul 28, 2011 10:53 am
Re: Finding an Element in a Rank 2 Array and Assignment
Code: Select all
To make changes: ((,6=Mat)/,Mat)←60
To find indices: (,6=Mat)/,⍳⍴Mat
-
- Posts: 94
- Joined: Sat Nov 28, 2009 3:12 pm
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:
-Veli-Matti
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
Or using array logic:
(⊢×10*6=⊢)3 3⍴⍳9
0 1 2
3 4 5
60 7 8
-
- Posts: 22
- Joined: Fri Aug 11, 2017 2:17 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.
- Adam|Dyalog
- Posts: 143
- Joined: Thu Jun 25, 2015 1:13 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 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
-
- Posts: 431
- Joined: Fri Oct 03, 2008 4:14 pm
Re: Finding an Element in a Rank 2 Array and Assignment
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.
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
This
Dyalog APL/W-64 Version 16.0.31693Might well have improved in later versions.
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% ⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕
- Adam|Dyalog
- Posts: 143
- Joined: Thu Jun 25, 2015 1:13 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!
The second expression (which needs ⎕IO←0) takes about 50% longer than ⍸ in version 17.0.
Try it online!
-
- Posts: 22
- Joined: Fri Aug 11, 2017 2:17 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.
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.