Finding all possible ways of adding array elements?

1 回表示 (過去 30 日間)
Daniel Derrick
Daniel Derrick 2016 年 1 月 12 日
コメント済み: Daniel Derrick 2016 年 1 月 12 日
A longer code I am writing is extremely time expensive, hopefully this approach will save a lot of time. The idea is to begin with, say, [1 1 1 1 1] And from left to right, find all the ways you can add the elements, ensuring that sum(V)=5 the whole while. The result is something like [0 1 1 1 2]; [0 1 1 2 1]; [0 1 2 1 1]; [0 2 1 1 1]; [0 0 1 1 3]; [0 0 1 2 1]; .... The idea is to create all possible combinations that have sum(row)=5, whilst cutting out repetitions as much as possible. I can filter out the remaining repetitions that will undoubtedly still come as an output using unique(V,'rows').
Anyone have any ideas? Kinda stuck right now.

回答 (1 件)

the cyclist
the cyclist 2016 年 1 月 12 日
Download this FEX submission and type
partitions(5,[1 1 1 1 1])
  1 件のコメント
Daniel Derrick
Daniel Derrick 2016 年 1 月 12 日
It's a bit slow- it creates many, many repetitions that are still slow to delete.

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

Community Treasure Hunt

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

Start Hunting!

Translated by