Pergunta

Eg I have array (/1,3,4,5,7,9,11/), how do I remove its 3rd element? I couldn't find an array function which does that, nor I find a loop an elegant solution, since I don't know how to append to an array (this means, add an element next to the previous defined element.)

I want to remove all even elements from an array... I know there is only one.

I can find its index using MINLOC, but I don't know how to remove an element from array.

Foi útil?

Solução

Let

a = (/1,3,4,5,7,9,11/)

then

pack(a,mod(a,2)/=0)

will return the odd elements of a. This isn't quite the same as removing the 3rd element, but your question suggests that removing the even element(s) is really what you want to do.

If you declare

integer, dimension(:), allocatable :: oddones

then

oddones = pack(a,mod(a,2)/=0)

will leave oddones containing the odd elements of a. You'll need an up-to-date compiler to use this automatic allocation.

Note that in Fortran, as in any sane language, arrays are of fixed size so removing an element isn't really supported. However, if a itself were allocatable then you could use a on the lhs of the expression. Let's leave it to the philosophers whether or not a remains the same under this operation.

Outras dicas

I have not used FORTRAN in a while, but look at the functions CSHIFT and EOSHIFT. I think you can't change the size of the array.

If you work with a fixed maximum size, but want dynamic behaviour within, you can just shift the elements:

integer, parameter :: max=10
integer :: a(max)
integer :: length_a, index, i
length_a = 8
a(1:length_a)=[1, 3, 4, 5, 7, 9, 11, 13]
!remove (third) element
index = 3
do i=index,length_a-1
  a(i)=a(i+1)
end do
length_a=length_a-1

The advantage is that this does not use an array temporary.

You could just do some slicing.

integer, parameter:: max = 10
integer used ! number of elements used
integer, dimension(1:max):: store
...
used = max
...
! Remove element n
if (n > 0 .and. n < used) then
    store(n:(used - 1)) = store((n+1):used)
end if
if (n > 0) used = used - 1

A working example, thanks to this discussion. Thanks, Dick Hendrickson and Dave Frank.

Quote:

joel GUERRERO wrote (Thursday, September 02, 2004 6:29 PM)

I have this question, how can I do to add a record or to delete a record from a list or array n fortran 90??

That is, let suppose that I have the following array:

(1 4 3 9 10 2 15 8)

And I want to add the value 13 in the 4th position in order to obtain:

(1 4 3 13 9 10 2 15 8)

You asked above several weeks back, which led to my asking the newsgroup what syntax F2003 adds to facilitate this operation.

Dick Hendrickson's response indicates below would work using F2003 syntax.

 integer,allocatable :: v(:) 
 v = [1, 4, 3, 9, 10, 2, 15, 8]
 v = [v(:3), 13, v(4:)]   ! insert 13 into expanded size v at v(4)

dave_frank 9/3/2004 8:07:59 AM

Working answer: as asked, removes 1 matching item only:

  program hello
     integer a(8)
     integer b(7)
     a=[1, 3, 4, 5, 7, 9, 11, 13]
     index = minloc(a, dim=1, mask=(mod(a, 2) .eq. 0))
     b=[a(1:index-1), a(index+1:size(a))]
     print *, b
  end program Hello

You may be able to loop, "while (index)" with additional edits as necessary, to remove all matches, if you like.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top