进出口寻找一种方式来添加两个数组的元素 [英] Im looking for a way to add up elements of two arrays

查看:135
本文介绍了进出口寻找一种方式来添加两个数组的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我期待的,我可以在一个数组,使得第一阵列的第一个元素被添加到每一个元件的第二阵列中,则第一阵列中的第二个元素被添加到所有每加起来元素的一种方式元件的第二阵列中等等。最终载体将是长度(a)*长度(B)的长

i am looking for a way that i can add up elements in an array such that the first element of the first array is added to every element in the second array, then the second element in the first array is added to all every element in the second array and so on. The final vector will be length(a)*length(b) long

例如...

A = [1,2,3,4] B = [5,6,7]
答案=
    [(1 + 5),(1 + 6),(1 + 7),(2 + 5),(2 + 6),(2 + 7),(3 + 5),(3 + 6),( 3 + 7),(4 + 5),(4 + 6),(4 + 7)]
   = [6,7,8,7,8,9,8,9,10,9,10,11]

推荐答案

我首先想到的是用匿名函数,增加的每一个标量元素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

这篇关于进出口寻找一种方式来添加两个数组的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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