how to split a vector into small subvectors based on condition
古いコメントを表示
how can i split a vector into smaller sub vectors, such that the sum of each vectors is less than N
N = 60
V = [30 35 24 15 14 48];
3 件のコメント
Walter Roberson
2020 年 3 月 15 日
Putting everything into the same vector satisfies the stated conditions.
Elysi Cochin
2020 年 3 月 15 日
Walter Roberson
2020 年 3 月 15 日
Breaking up into individual elements satisfies the stated conditions. There are other solutions too, but the question does not prevent the algorithm from being lazy and not even trying a different solution.
採用された回答
その他の回答 (1 件)
Ahmed Anas
2020 年 3 月 15 日
編集済み: Ahmed Anas
2020 年 3 月 15 日
Dear, it will give you the desired results
clear all
clc
V = [30 35 24 15 14 48]
N=60
for i=1:size(V,2)
subsA = nchoosek(V,i);
for j=1:size(subsA)
Sum=sum(subsA(j,:));
if Sum<N
G=subsA(j,:)
end
end
end
3 件のコメント
Ahmed Anas
2020 年 3 月 15 日
If you could not understand this code then please tell..
Walter Roberson
2020 年 3 月 15 日
I suspect that the sub-vectors are intended to be consecutive elements.
Elysi Cochin
2020 年 3 月 15 日
カテゴリ
ヘルプ センター および File Exchange で Matrix Indexing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!