apply bsxfun or arrayfun to every row of a matrix

前端 未结 2 1589
面向向阳花
面向向阳花 2021-01-19 02:28

There are two matrices, A and B with size m-by-4 and n-by-4 respectively. My question is how to apply a function f<

2条回答
  •  北恋
    北恋 (楼主)
    2021-01-19 03:13

    You can use arrayfun, if you first use meshgrid to generate all combinations of rows:

    [ii jj] = meshgrid(1:size(A,1),1:size(B,1));
    result = arrayfun(@(n) rectint( A(ii(n),:), B(jj(n),:) ), 1:numel(ii) );
    result = reshape(result, size(B,1), size(A,1)).';
    

    You could of course substitute rectint by any other function that accepts two vector inputs and returns a number.

提交回复
热议问题