How to subtract an element of an array from every element in another array in Fortran? -
i have 2d array called zeta_list containing 100 x , y coordinate values. have 2d array called x. want subtract first x coordinate in list every element in x array, , carry on subtract till nth x coordinate via loop in fortran. when try this, error: inconsistent ranks operator @ (1) , (1). code follows:
program lensingtest1 double precision,dimension(1,1000)::m_list double precision,dimension(100,100)::x,y,z_m_z_x,z_m_z_y,dist_z_m_z, alpha_x, alpha_y double precision,dimension(2,1000)::zeta_list open(9,file='/home/amruth/desktop/coding/fortran/test_programs/lensing_test/zeta_list.txt') open(8,file='/home/amruth/desktop/coding/fortran/test_programs/lensing_test/x.txt') read(9,*)zeta_list read(8,*)x write(*,*) x - zeta_list(1,1:1)#this line causes error !the loop might !do i=1,size(m_list,1),1 !z_m_z_x = x - zeta_list(1,i:i) !end end program lensingtest1
when explicitly check value zeta_list(1,1:1), single scalar value don't see why should getting error can subtract scalar values entire array such x.
i think comments allude -- you're referencing 1x1 array element fortran not understand scalar, though is. agentp provides 1 option confronting this,
z_m_z_x = x - sum(zeta_list(1,:))
i similar, different in semantic manner agentp's suggestion
z_m_z_x= x(i,1) - maxval(zetalist(1,:))
in each case you're transforming array scalar via 1) summing elements in array, 1 or 2) picking maxvalue of elements in array, again max of 1 value. minval work equally well. however, if understand question right --- have m x 2 array 'x' trying subtract out of values first column of zeta_list ---
then there might way vectorize operation. if trying subtract x values of first column of 2 x m array, , arrays both had m rows, simply
x(:,1)=x(:,1) - zetalist(:,1) x(:,2)=x(:,2) - zetalist(:,1)
however, approach work if arrays of same number of rows- specific range require slicing equivalent number of rows in each array slicing right columns. if trying subtract 1,1 array element every location in x , want use loop write
i=1,m x(i,1) = x(i,1)- zetalist(1,1) x(i,2) = x(i,2)- zetalist(1,1) end
Comments
Post a Comment