[英]Matlab arrayfun mapping using two arrays?
How would I go about mapping the values of TWO arrays to a function and returning the result as an array? 我该如何将两个数组的值映射到函数并将结果作为数组返回?
arr = [1, 2, 3];
arr2 = [1, 4, 5];
val= arrayfun(@(x) func(arr, arr2))
function val = func(x, y)
// Takes in two arrays, and does a double 'for' loop
// for all values in x, for all values in y, do x*y
So basically I would end up with an array of XY val: 因此,基本上我将得到一个XY val数组:
1 1 1 2 2 2 3 3 3 <- X values
1 4 5 1 4 5 1 4 5 <- Y values
1*1, 1*4, 1*5, 2*1, 2*4, 2*5, 3*1, 3*4, 3*5 <- X*Y values
Thanks. 谢谢。
EDIT - Updated desired output array 编辑-更新了所需的输出数组
arr=[1,2,3];
arr2=[1,4,5];
product=zeros(1,9);
index=1;
for i=1:length(arr)
for j=1:length(arr2)
product(index)=arr(i)*arr2(j);
index=index+1;
end
end
try the above code. 试试上面的代码。
You could proceed this way: 您可以这样进行:
res = arrayfun(@(i) arr(i).*arr2,1:numel(arr),'UniformOutput',0);
final_res = cat(2,res{:});
I hope this helps. 我希望这有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.