Permutations using multiple vectors of different lengths

I am trying to sum values of all permutations of values from multiple matrices of different lengths, and cannot find a way to use perms or for loops to do it.
v1=[10,600,800];
v2=[6,13];
v3=[5,2];
v4=[10,15,22,6];
v5=[12,5];
I want each matrix to contribute one (and only one) value to the permutation. Ultimately, I'm looking for a list that shows something like the following:
10+6+5+10+12=43
10+6+5+10+5=36
10+6+5+15+12=45
...
800+13+2+6+5=826
Even better would be if I could figure out how to get it to show the indexes used for each one as follows:
(1)+(1)+(1)+(1)+(1)=43
(1)+(1)+(1)+(1)+(2)=36
(1)+(1)+(1)+(2)+(1)=45
...
(3)+(2)+(2)+(4)+(2)=826
Any help is greatly appreciated. Thank you.

2 件のコメント

John D'Errico
John D'Errico 2019 年 9 月 9 日
編集済み: John D'Errico 2019 年 9 月 9 日
help ndgrid
Think about what it does.
Robert Rieke
Robert Rieke 2019 年 9 月 9 日
ndgrid may work, but I'm having a difficult time understanding how it would be implemented.

サインインしてコメントする。

 採用された回答

David Hill
David Hill 2019 年 9 月 9 日

1 投票

If the number of matrixes does not change (in this case 5 different maxtrixes,a b,c,d,e), for-loops could be used this way:
output=zeroes(1,length(a)*length(b)*length(c)*length(d)*length(e));
count=1;
for i1=a
for i2=b
for i3=c
for i4=d
for i5=e
output(count)=i1+i2+i3+i4+i5;
count=count+1;
end
end
end
end
end

2 件のコメント

David Hill
David Hill 2019 年 9 月 9 日
If you want indexes, this output produces each index used to obtain the sum.
function output = sumPermMatrixes(a,b,c,d,e)
output=zeros(length(a)*length(b)*length(c)*length(d)*length(e),6);
count=1;
for i1=1:length(a)
for i2=1:length(b)
for i3=1:length(c)
for i4=1:length(d)
for i5=1:length(e)
output(count,:)=[i1,i2,i3,i4,i5,a(i1)+b(i2)+c(i3)+d(i4)+e(i5)];
count=count+1;
end
end
end
end
end
end
Robert Rieke
Robert Rieke 2019 年 9 月 9 日
Those answers worked well, thank you! The code in the first version should have "zeros" instead of "zeroes", but that's not a big deal.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by