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!