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 Comments

Putting everything into the same vector satisfies the stated conditions.
very sorry, question edited, should not be greater was my question
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.

Sign in to comment.

 Accepted Answer

From your other question: https://www.mathworks.com/matlabcentral/answers/510716-compute-from-a-set-of-parameters. I suspect that you want to find all distinct combination of elements where the sum is less than 60 while keeping the number of trips to a minimum. The following code will find an optimal solution; however, it will only work if the number of elements of V is small (say less than 20). The solution have exponential time and space complexity, so the required resources will grow very quickly. For large number of variables, I would recommend using some greedy method, which can give a sub-optimal solution.
N = 60;
V = [30 35 24 15 14 48];
a = mat2cell(repmat([0 1], numel(V), 1), ones(size(V)), 2);
combs = logical(combvec(a{:})'); % create all possible combinations
combs(1, :) = []; % remove a trivial combination
cost = sum(combs.*V, 2);
valid_index = cost < N;
valid_combs = combs(valid_index, :);
valid_costs = cost(valid_index);
[valid_costs, index] = sort(valid_costs, 'descend');
valid_combs = valid_combs(index, :);
optimal_combs = logical([]);
while ~isempty(valid_combs)
current_comb = valid_combs(1,:);
optimal_combs = [optimal_combs; current_comb];
index = any(valid_combs(:, current_comb) == valid_combs(1, current_comb), 2);
valid_combs(index, :) = [];
end
result = {};
for i=1:size(optimal_combs,1)
result{i} = V(optimal_combs(i,:));
end

6 Comments

Sir why dont you post this answer in that link, so that i will accept it there, and it will add to your points
As i didnt get an answer to that question, i thought of splitting the question in a simple way
sir, the first Answer you gave, when i executed the code, its somewhat close,
i dont want repeatition of elements, once if i store a value into a new subvector , then i wont consider that value
my expected output is
{ [ 30 15 14] [ 35 24 ] [ 48] }
or do i need to use the second Answer to get the desired output?
Elysi, my current code also gives the output you require. Check the value of result variable
result{1}
result{2}
result{3}
You will see there is no repetition. I posted this code in response to your other question too.
Sir i was telling about repetition in this code
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
Elysi, that answer was not from me. It was posted by Ahmed Anas.
sorry sir for the confusion, now only saw the names were different
Thank you sir for your answers
No problem. Gald to be of help.

Sign in to comment.

More Answers (1)

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 Comments

If you could not understand this code then please tell..
I suspect that the sub-vectors are intended to be consecutive elements.
sir, i executed the code, its somewhat close,
i dont want repeatition of elements, once if i store a value into a new subvector , then i wont consider that value
my expected output is
{ [ 30 15 14] [ 35 24 ] [ 48] }

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!