Piet,

I only receive the "Digest" from programming every so often so my reply is
later than others.  On top of that I have discovered another issue of getting
through, so my reply here is a bit stale. However I include the original
blocked and late message for the record.


The solution below works provided you want to maintain the order of
all "OTHER" axes - I have just read Henry's discussion and agree with him.

I think if you want the axes changed position as well then the
appropriate way to go is to use either     {"_ r       (to insert results
in place of axis r counting backward) or   {"0 r"0 _    (see discussion)
AND then as the next step change the axes around to suit
your need/preference.



================

From your description, it appears you want to sequentially select (arrays of
Some determined rank) from a larger dimensional array, and stack them
all up.

indexList {"0 r"0 _ ARRAY

where r is the rank you want to operate with, will do this.
You can make this expression into a (slightly complicated) adverb and
give it a name if that’s your wish.

Using Rank twice does take some time to get your head around, but it is
very useful on occasions.

Here the outer rank adverb ("0 _), says take the indexes as scalars
(rank 0) , apply with all (rank infinity) the M array to the derived
verb ({"0 r) and then STACK the results.

The inner verb (  {"0 r  ) says index using scalars (it has no choice
here as it only will be given scalars) into rank r sub arrays. This just
picks off along the FIRST dimension of the sub array. And then stack
all these sub-array as determined by the outer rank of M.  That is, if
(as example  below) the array has dimension  6 7 8 9 10  and we are
indexing into rank 3 sub arrays using the expression

   5 2 {"0 3"0 _ M

then there is a 6 x 7 stack of these results for the   {"0 3   verb to
produce. And as this verb was given a scaler the indexing produces
just a 2D result in each application.
That is for each of the 84 times the  {  verb is invoked using the
above expression it is of the form  scalar { 3Darray  producing a
2Darray.

NOTE that in J your code tells J *WHAT* to do  not necessarily
*HOW* to do it. There are many many smarts especially with
rank support.


========

   $ M =. 6 7 8 9 {. i. 10 10 10 10 10
6 7 8 9 10

NB. index into last dimension and stack into new first dimension
   $ 5 2 {"0 1"0 _ M
2 6 7 8 9


NB. index into last two dimensions and stack into new first dimension
   $ 5 2 {"0 2"0 _ M
2 6 7 8 10

   $ 5 2 {"0 3"0 _ M
2 6 7 9 10

   $ 5 2 {"0 4"0 _ M
2 6 8 9 10

   $ 5 2 {"0 5"0 _ M
2 7 8 9 10

   $ (,5){"0 4"0 _ M
1 6 8 9 10

   $ 5   {"0 4"0 _ M     NB. index is scalar
6 8 9 10

========

The dimension specified by  r (counting from the back end) is removed
from the array, and replaced by a new first dimension of length
determined by the index list.


Ian

Ian Shannon
Undertaking rigorous statistics in support of the NSW environment
For every complex problem there is an answer that is clear, simple, and wrong.
                                                                         
Mencken 

> Date: Thu, 7 Sep 2023 07:12:21 +1000
> From: Piet de Jong <pietd...@gmail.com>
> To: Programming forum <programm...@jsoftware.com>
> Subject: [Jprogramming] Rank difficulties
> Message-ID: <9396652b-1b95-4aa9-ab8e-821bcc321...@gmail.com>
> Content-Type: text/plain;     charset=utf-8
> 
> Apologies if this is an ignorant question.  (Always learning)
> 
> I want to take “slices” of a multidimensional array.
> For simplicity suppose the array is 2 dimensional (This is not my 
> actual situation but illustrates my difficulties)
> 
> m=. i.2 2
> 
> The two horizontal slices are 0{m and 1{m or more simply 0 1{m
> 
> The two vertical slices are 0{“1 m and 1{“1 m
> 
> But 0 1{“1 m yields m rather than (0{"1 m),:1{"1 m as I want.
> 
> I know this can be achieved with say 0 1{|:m
> 
> But (given my more intricate situation) how to do this using rank?
> 
> In general, given an n dimensional array how to get the successive 
> slices of the array along the successive dimensions as  successive 
> items of the resulting array using the rank adverb?
> 
> 
> 
> 
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
This email is intended for the addressee(s) named and may contain confidential 
and/or privileged information. 
If you are not the intended recipient, please notify the sender and then delete 
it immediately.
Any views expressed in this email are those of the individual sender except 
where the sender expressly and with authority states them to be the views of 
the NSW Office of Environment and Heritage.

PLEASE CONSIDER THE ENVIRONMENT BEFORE PRINTING THIS EMAIL
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to