使用bsxfun与匿名函数 [英] Using bsxfun with an anonymous function

查看:215
本文介绍了使用bsxfun与匿名函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图了解bsxfun功能我试图实现它在脚本中,以避免循环之后。我想,以检查是否在一个阵列中的每个单独的元件被包含在一个矩阵,返回的矩阵相同大小分别为1和0的含有初始阵列。我创建了匿名函数是:

after trying to understand the bsxfun function I have tried to implement it in a script to avoid looping. I am trying to check if each individual element in an array is contained in one matrix, returning a matrix the same size as the initial array containing 1 and 0's respectively. The anonymous function I have created is:

myfunction = @(x,y) (sum(any(x == y)));

x是一个包含每说'接受的价值'的矩阵。 y是输入数组。到目前为止,我已经使用bsxfun功能这样的尝试:

x is the matrix which will contain the 'accepted values' per say. y is the input array. So far I have tried using the bsxfun function in this way:

dummyvar = bsxfun(myfunction,dxcp,X)

据我所知,myFunction是等于匿名函数的句柄和bsxfun可以用来完成此我只是不明白以下错误的原因:

I understand that myfunction is equal to the handle of the anonymous function and that bsxfun can be used to accomplish this I just do not understand the reason for the following error:

Non-singleton dimensions of the two input arrays must match each other. 

我使用下面的测试数据:

I am using the following test data:

dxcp = [1 2 3 6 10 20];
X = [2 5 9 18];

和希望的输出是:

dummyvar = [1,0,0,0]

干杯,NZBRU。

Cheers, NZBRU.

编辑:达到了15代表处,所以我已经更新了答案

推荐答案

再次谢谢你们,我以为我会更新这个,因为我现在明白了,从Divakar提供的解决方案是如何工作的。这可能跟其他人谁看过我最初的问题,都搞不清楚到bsxfun()是如何工作的,我觉得写出来帮助我更好地理解这一点。

Thanks again guys, I thought I would update this as I now understand how the solution provided from Divakar works. This might deter confusion from others who have read my initial question and are confused to how bsxfun() works, I think writing it out helps me understand it better too.

注意:以下可能是不正确的,我刚才想明白是怎么函数通过观察此一例操作

进入bsxfun函数的输入是dxcp和X换位。使用该功能手柄是@eq所以每个元素进行比较。

The input into the bsxfun function was dxcp and X transposed. The function handle used was @eq so each element was compared.

%%// Given data
dxcp = [1 2 3 6 10 20];
X = [2 5 9 18];

以下code:

bsxfun(@eq,dxcp,X')

相比dxcp的每一个值,第一输入变量,于X的每一行'。下面的表格是这样的输出:

compared every value of dxcp, the first input variable, to every row of X'. The following matrix is the output of this:

dummyvar =

 0     1     0     0     0     0
 0     0     0     0     0     0
 0     0     0     0     0     0
 0     0     0     0     0     0

的第一个元素是通过比较1和2 dxcp发现= [ 1 2 3 6 10 20]; X'= [ 2 ; 5; 9; 18];

The first element was found by comparing 1 and 2 dxcp = [1 2 3 6 10 20]; X' = [2;5;9;18];

沿着第一排的下一个通过比较2和2 dxcp发现= [1 2 第3 6 10 20]; X'= [ 2 ; 5; 9; 18];

The next along the first row was found by comparing 2 and 2 dxcp = [1 2 3 6 10 20]; X' = [2;5;9;18];

此重复,直到所有的地方相比,第一行的X'dxcp的值。以下这样的逻辑,在第二行中的第一个元素是使用之间的比较计算:dxcp = [<强> 1 2 3 6 10 20]; X'= [2; 5 ; 9; 18];

This was repeated until all of the values of dxcp where compared to the first row of X'. Following this logic, the first element in the second row was calculating using the comparison between: dxcp = [1 2 3 6 10 20]; X' = [2;5;9;18];

提供的最终溶液是任何(bsxfun(@当量,dxcp,X'),2),其等效于:任何(dummyvar,2)。 http://nf.nci.org.au/facilities /software/Matlab/techdoc/ref/any.html 似乎可以解释详细的功能良好。基本上,说:

The final solution provided was any(bsxfun(@eq,dxcp,X'),2) which is equivalent to: any(dummyvar,2). http://nf.nci.org.au/facilities/software/Matlab/techdoc/ref/any.html seems to explain the any function in detail well. Basically, say:

A = [1,2;0,0;0,1]

如果以下code运行:

If the following code is run:

result = any(A,2)

然后函数任何将检查是否每行包含一个或多个非零元素并若有返回1。本实施例的结果将是:

Then the function any will check if each row contains one or several non-zero elements and return 1 if so. The result of this example would be:

result = [1;0;1];

由于如果上述线被改变,导致第二个输入参数是等于2 =任何(A,1),那么它会检查每一列

Because the second input parameter is equal to 2. If the above line was changed to result = any(A,1) then it would check for each column.

使用这个逻辑,

result = any(A,2)

用于获得最终结果。

was used to obtain the final result.

1
0
0
0

这如果需要,可以移等于

which if needed could be transposed to equal

[1,0,0,0]

性能 - 运行以下code之后:

Performance- After running the following code:

tic
dummyvar = ~any(bsxfun(@eq,dxcp,X'),2)' 
toc

已经发现,持续时间为:

It was found that the duration was:

Elapsed time is 0.000085 seconds.

以下选择:

tic
arrayfun(@(el) any(el == dxcp),X)
toc

使用arrayfun()函数(适用函数对数组中的每个元素)

导致在运行时:

using the arrayfun() function (which applies a function to each element of an array) resulted in a runtime of:

Elapsed time is 0.000260 seconds.

^上面运行时间超过的每个含义5次运行的平均值,在这种情况下,bsxfun()被快(平均)

^The above run times are averages over 5 runs of each meaning that in this case bsxfun() is faster (on average).

这篇关于使用bsxfun与匿名函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆