且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

进出口寻找一种方式来添加两个数组的元素

更新时间:2023-02-05 12:32:15

我首先想到的是用匿名函数,增加的每一个标量元素B中的全阵列arrayfun做到这一点。然后,因为你得到一个单元阵列导致你可以扩展单元阵列到您正在寻找的数组:

My first thought is to do this with arrayfun using an anonymous function that adds each scalar element of a to the full array in b. Then since you get a cell array result you can expand that cell array into the array you are looking for:

>> a=[1,2,3,4], b=[5,6,7]
>> result = arrayfun(@(x) x+b, a,'UniformOutput',false);
>> result = [result{:}]

result =

     6     7     8     7     8     9     8     9    10     9    10    11