簡體   English   中英

是否有與 matlab ismember 函數等效的 Fortran 語言?

[英]Is there a fortran equivalent of the matlab ismember function?

matlab 函數 ismember 是否有任何等效的 Fortran 語言? 在 Matlab 中,我的代碼是:

A = [1 0 0 1;0 1 1 0;0 1 1 0;];
B = [0 1 1 0;1 0 0 1;0 0 0 0;];

l = size(A,1);
for i = 1:l
    I = ismember(A,B(i,:),'rows');
    indx = find (I == 1)
end

其中的輸出是:

indx =

     2
     3


indx =

     1


indx =

  0×1 empty double column vector

基本上,在我的例子中,ismember 所做的是檢查矩陣 B 的每一行,並驗證矩陣 A 中是否有匹配的行。

- - - - - - - - - - - - 編輯 - - - - - - - - - - - -

我用 Fortran 編寫了以下程序。 第一部分使用 2 個 do 循環並給出所需的輸出。 我寫第二部分是為了將循環數減少為一個。 但是,后者不提供所需的輸出。 你能告訴我應該如何修改第二部分才能得到想要的結果嗎?

program ismember
  implicit none

  integer :: i,j, i_ind, ii
  integer, parameter :: N_lines = 5, N_columns = 3 
  integer, allocatable :: AA1(:,:),AA4(:,:)
  integer, allocatable :: indices(:)
  logical :: row_wanted2(N_lines)

  ! --------------------------- define the matrix AA1 ---------------------------!
  Allocate(AA1(N_lines,N_columns))
  do i=1,N_lines
     do j=1,N_columns
        AA1(i,j)= i + j 
     end do
  end do

  print*, 'AA1'
  do i = 1, N_lines
     write(*,"(1X,10I5)")  (AA1(i,j), j = 1, N_columns)
  end do

  ! --------------------------- define the matrix AA4 ---------------------------!
  Allocate(AA4(N_lines,N_columns))
  AA4(1,:) = AA1(5,:)
  AA4(3,:) = AA1(1,:)
  AA4(5,:) = AA1(3,:)
  AA4(2,:) = 0
  AA4(4,:) = 0  
  print*, 'AA4'
  do i = 1, N_lines
     write(*,"(1X,10I5)")  (AA4(i,j), j = 1, N_columns)
  end do

  ! --------------------------- ismember ---------------------------!
  print*, 'AA1 #lines', size(AA1,1)
  print*, 'AA1 #columns', size(AA1,2)

  row_wanted2 = .true.

  ! --------------------------- First part ---------------------------!  
  do i = 1, size(AA1,1)

     print*, i
     do ii = 1, size(AA4,1)
        if ( all( AA1(i,:) == AA4(ii,:) ) ) then
           print *, "AA1 and AA4 have same shape"
        else
           print *, "AA1 and AA4 have different shape"
        end if
     end do

  end do

  ! --------------------------- Second part ---------------------------!  
  do i = 1, size(AA1,1)
     
     indices = pack([(i_ind,i_ind=1,size(AA1,1))], all(AA1(i,:) == AA4(i_ind,:)) )
     if(size(indices).ge.1) then
        print*, 'indices', indices
        print*, 'we have a match'
     else
        row_wanted2(i) = .false.
        print*, 'we do not have a match'
     endif
     deallocate(indices)
     
  end do

  
end program ismember 

下面是 Matlab 函數ismember(a,b,'rows')的等價物(我還沒有嘗試編譯它,所以可能有一些錯誤需要更正):

function ismember_rows(a,b) result(c)
implicit none
real, intent(in) :: a(:,:), b(:)
logical, allocatable :: c(:)

integer :: n, m, i

n = size(a,1)
m = size(a,2)
if (size(b) /= m) stop "b size is wrong"

allocate( c(n) )
do i = 1, n
    c(i) = all( b(:) == a(i,:) )
end do

end function ismember_rows

實際上非常簡單,只需迭代a的行並將它們與b進行比較。 如果 a 的第 i 行a所有元素都等於b的元素,則c(i)為真。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM