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

Postby REMINGTON30 on Tue Aug 28, 2018 6:04 pm

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: 22
Joined: Fri Aug 11, 2017 2:17 pm

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

Postby Roger|Dyalog on Tue Aug 28, 2018 7:10 pm

Code: Select all
To make changes:  ((,6=Mat)/,Mat)←60
To find indices:  (,6=Mat)/,⍳⍴Mat
Roger|Dyalog
 
Posts: 238
Joined: Thu Jul 28, 2011 10:53 am

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

Postby Veli-Matti on Wed Aug 29, 2018 5:40 am

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
Veli-Matti
 
Posts: 93
Joined: Sat Nov 28, 2009 3:12 pm

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

Postby Phil Last on Wed Aug 29, 2018 7:21 am

Or using array logic:
      (⊢×10*6=⊢)3 3⍴⍳9
0 1 2
3 4 5
60 7 8
User avatar
Phil Last
 
Posts: 624
Joined: Thu Jun 18, 2009 6:29 pm
Location: Wessex

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

Postby REMINGTON30 on Wed Aug 29, 2018 6:28 pm

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: 22
Joined: Fri Aug 11, 2017 2:17 pm

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

Postby Adam|Dyalog on Sun Sep 02, 2018 6:56 am

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
User avatar
Adam|Dyalog
 
Posts: 134
Joined: Thu Jun 25, 2015 1:13 pm

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

Postby paulmansour on Sun Sep 02, 2018 8:16 pm

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.
paulmansour
 
Posts: 418
Joined: Fri Oct 03, 2008 4:14 pm

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

Postby Phil Last on Sun Sep 02, 2018 10:01 pm

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.
User avatar
Phil Last
 
Posts: 624
Joined: Thu Jun 18, 2009 6:29 pm
Location: Wessex

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

Postby Adam|Dyalog on Mon Sep 03, 2018 5:44 am

Hi Phil,

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

Try it online!
User avatar
Adam|Dyalog
 
Posts: 134
Joined: Thu Jun 25, 2015 1:13 pm

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

Postby REMINGTON30 on Wed Sep 05, 2018 7:13 pm

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: 22
Joined: Fri Aug 11, 2017 2:17 pm


Return to New to Dyalog?

Who is online

Users browsing this forum: No registered users and 1 guest