Array Definition

  • Follow


How would you define the following array a=(/
1,2,13,1,2,13,1,2,13,1,2,13,../) if the repeating unit (/1,2,13/)
appears N times where N is known.
0
Reply makis (6) 9/13/2010 10:13:50 AM

Hi Michael,

Michael wrote:
> How would you define the following array a=(/
> 1,2,13,1,2,13,1,2,13,1,2,13,../) if the repeating unit (/1,2,13/)
> appears N times where N is known.

I would type it out. You might be able to play silly burgers with 
reshape (haven't thought how), but given the standard only allows up to 
7 dimensional arrays N better be 0, 1 or 2, so typing it out would be 
both not very long and clearer.

But what are you trying to do? I strongly suspect something to do with 
derived types might be better, but given this info I can't guess what,

Ian
0
Reply Ian 9/13/2010 10:37:02 AM


On 13/09/2010 8:13 PM, Michael wrote:
> How would you define the following array a=(/
> 1,2,13,1,2,13,1,2,13,1,2,13,../) if the repeating unit (/1,2,13/)
> appears N times where N is known.

program Oi_Terence_try_and_do_this_shorter_in_f77
   implicit none
   integer, parameter :: ru(3) = [1, 2, 13]   ! the repeating unit
   integer, allocatable :: a(:)
   integer :: i, n
   write (*,"('Enter n:')")
   read (*,*) n
   a = [(ru, i = 1, n)]   ! F2003 allocate on assignment.
   write (*, *) a         ! Replace with user defined operation on a...
end

0
Reply Ian 9/13/2010 10:44:04 AM

Ian Bush wrote:
> 
> Hi Michael,
> 
> Michael wrote:
>> How would you define the following array a=(/
>> 1,2,13,1,2,13,1,2,13,1,2,13,../) if the repeating unit (/1,2,13/)
>> appears N times where N is known.
> 
> I would type it out. You might be able to play silly burgers with 
> reshape (haven't thought how), but given the standard only allows up to 
> 7 dimensional arrays N better be 0, 1 or 2, so typing it out would be 
> both not very long and clearer.
> 
> But what are you trying to do? I strongly suspect something to do with 
> derived types might be better, but given this info I can't guess what,
> 

What a load of rubbish - Ignore this, I obviously can't read,

Ian
0
Reply Ian 9/13/2010 11:01:59 AM

Michael wrote:
> How would you define the following array a=(/
> 1,2,13,1,2,13,1,2,13,1,2,13,../) if the repeating unit (/1,2,13/)
> appears N times where N is known.

Do you mean (/ (1,2,13,i=1,N) /) ?

0
Reply Michel 9/13/2010 12:31:15 PM

Putting Michel's solution in a working program:

program blah
  implicit none
  integer, parameter :: N = 5
  integer, parameter :: NR = 3
  integer, parameter :: R(NR) = (/1,2,13/)
  integer :: i
  integer :: arr(N*NR) = (/ (R,i=1,N) /)
  write(*,'(10i5)') arr
end program blah

lnx: gfortran blah.f90
lnx: a.out
    1    2   13    1    2   13    1    2   13    1
    2   13    1    2   13

cheers,

paulv



Michel Olagnon wrote:
> Michael wrote:
>> How would you define the following array a=(/
>> 1,2,13,1,2,13,1,2,13,1,2,13,../) if the repeating unit (/1,2,13/)
>> appears N times where N is known.
> 
> Do you mean (/ (1,2,13,i=1,N) /) ?
> 
0
Reply Paul 9/13/2010 7:20:38 PM

"Michel Olagnon" <molagnon@ifremer-a-oter.fr> wrote in message 
news:4C8E1993.3060508@ifremer-a-oter.fr...

> Michael wrote:

>> How would you define the following array a=(/
>> 1,2,13,1,2,13,1,2,13,1,2,13,../) if the repeating unit (/1,2,13/)
>> appears N times where N is known.

> Do you mean (/ (1,2,13,i=1,N) /) ?

I was considering the possibility that N might not be integral.
Solution:

a = reshape((/integer::/),shape(a),pad=(/1,2,13/))

-- 
write(*,*) transfer(0.64682312090346863D-153,(/'X'/));end


0
Reply James 9/13/2010 9:32:06 PM

On Sep 13, 11:32=A0pm, "James Van Buskirk" <not_va...@comcast.net>
wrote:
> "Michel Olagnon" <molag...@ifremer-a-oter.fr> wrote in message
>
> news:4C8E1993.3060508@ifremer-a-oter.fr...
>
> > Michael wrote:
> >> How would you define the following array a=3D(/
> >> 1,2,13,1,2,13,1,2,13,1,2,13,../) if the repeating unit (/1,2,13/)
> >> appears N times where N is known.
> > Do you mean (/ (1,2,13,i=3D1,N) /) ?
>
> I was considering the possibility that N might not be integral.
> Solution:
>
> a =3D reshape((/integer::/),shape(a),pad=3D(/1,2,13/))
>
> --
> write(*,*) transfer(0.64682312090346863D-153,(/'X'/));end

This does it too:

  integer, parameter :: NUM =3D 3
  integer :: n =3D NUM
  integer, dimension (NUM*3) :: m
  integer, dimension(3) :: source =3D [1, 2, 13]
!
  m =3D reshape (spread (source, 2, n), [n * size (source, 1)])
!
  print *, m
end

It's all a matter of taste and inclination :-)
Paul Thomas
0
Reply Paul 9/15/2010 7:38:15 AM

7 Replies
213 Views

(page loaded in 0.096 seconds)

Similiar Articles:













7/6/2012 6:33:41 AM


Reply: