apply bsxfun or arrayfun to every row of a matrix

狂风中的少年 提交于 2019-12-01 17:47:54
Luis Mendo

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.

This functionality is built into rectint. For the syntax rectint(A,B):

A and B can also be matrices, where each row is a position vector. AREA is then a matrix giving the intersection of all rectangles specified by A with all the rectangles specified by B. That is, if A is M-by-4 and B is N-by-4, then AREA is an M-by-N matrix...

So, you can just do result = rectint(A,B); to get the matrix you are after.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!