comp.lang.ada
 help / color / mirror / Atom feed
* on using array index, vectorized operation
@ 2011-03-27  1:31 Nasser M. Abbasi
  2011-03-27 18:48 ` Nasser M. Abbasi
  2011-03-27 20:01 ` Cyrille
  0 siblings, 2 replies; 20+ messages in thread
From: Nasser M. Abbasi @ 2011-03-27  1:31 UTC (permalink / raw)


Hello;

Many times I need to operate on an array using an index
by doing some operation on the index itself as a variable
(such as shifting for example).

This below is a very simple example of what I mean.

Given an array A, I might want to do

A(i-1) := A(i)    ----------- (1)

where 'i' above is a variable which contain in it some index
locations to use to index into the array A.

In Ada, I can do the above, but I am a little clumsy in doing
it. I know I can use array slicing as in  A(1..4) := A(2..5),
but wanted to do it when the index itself is a variable, as in (1)
above. Since in other cases, I have to do more operations on the index
itself, and so it needs to be in a variable.

This is what I tried so far, and my goal to see if I can
shorten this more:

------------------------
procedure t is
i: constant array (1..4) of integer:=(2,3,4,5);
A: array(1..5) of integer := (10,20,30,40,50);

begin
   --A(2..5) := A(1..4);
   --A(i-1) := A(i);  --error

   A(i(1)-1..i(4)-1) := A(i(1)..i(4));
end t;
---------------------

Now, when I print A, it will show (20,30,40,50,50)
as expected.

But how can I change the above to do it more directly, as (1)?

In Fortran, I can do it as follows:

--------------------
program t
implicit none
integer :: A(1:5)=(/10,20,30,40,50/)
integer :: i(1:4)=(/2,3,4,5/)

A(i-1) = A(i)

end program t
---------------------

and in Matlab also

----------------------
EDU>> A=[10 20 30 40 50];
EDU>> i=2:5;
EDU>> A(i-1)=A(i)
A =
     20    30    40    50    50
-------------------------

I am sure it is possible also in Ada to make this operation
as short in syntax as it is in the other 2 systems I showed above.
I just was not able to figure the syntax yet.

thanks
--Nasser



^ permalink raw reply	[flat|nested] 20+ messages in thread

end of thread, other threads:[~2011-03-30 13:00 UTC | newest]

Thread overview: 20+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-03-27  1:31 on using array index, vectorized operation Nasser M. Abbasi
2011-03-27 18:48 ` Nasser M. Abbasi
2011-03-27 20:01 ` Cyrille
2011-03-27 20:44   ` Nasser M. Abbasi
2011-03-27 21:02     ` Pascal Obry
2011-03-27 21:30     ` Shark8
2011-03-27 22:00       ` Nasser M. Abbasi
2011-03-27 22:37         ` Phil Clayton
2011-03-27 22:43           ` Nasser M. Abbasi
2011-03-27 22:59             ` Phil Clayton
2011-03-27 22:09     ` Phil Clayton
2011-03-27 22:12       ` Nasser M. Abbasi
2011-03-27 22:23       ` Nasser M. Abbasi
2011-03-29  2:50         ` Randy Brukardt
2011-03-29  4:55           ` Nasser M. Abbasi
2011-03-29 20:11             ` Phil Clayton
2011-03-29 21:17               ` Nasser M. Abbasi
2011-03-29 22:49                 ` Phil Clayton
2011-03-29 23:47                   ` Nasser M. Abbasi
2011-03-30 13:00                     ` Robert A Duff

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox