How do I write a for loop for repeatable division algorithm?

2 views (last 30 days)
Hi all,
"a" contains index, Vb contains values
for example,
inputs: vref= 20 a=[2 4 6 9] Vb=[ 8 9 1 7 5 3 6 55 8 7]
output: (9 plus with 1 gives 16) sum_Bank1 = 16
What I want to do:
If sum_Bank1 is greater than vref, i want the system to divide Bank1 into half again, and check if sum_Bank3 is still greater than vref, if it is, Bank3 is divided by half again, this process is repeated for many times until certain condition.
My question is, how can I write it in a loop instead of repeat the same codes many times?
function [sum_Bank1] = division(a, vref)
dimen = size (a);
ncol = dimen (2); % ncol=4
Bank1= a(:, 1:ncol/2); % Bank1 = [2 4]
Bank2= a(:, ((ncol/2) + 1):ncol); % Bank2 = [6 9]
sum_Bank1=sum(Vb(Bank1)); % sum_Bank1 = 16
sum_Bank2=sum(Vb(Bank2)); % sum_Bank1 = 11
if sum_Bank1 > vref
Bank3= Bank1(:,1:ncol/2);
Bank4= Bank1(:, ((ncol/2) + 1):ncol);
if sum_Bank3 > vref
Bank5= Bank3(:,1:ncol/2);
Bank6= Bank3(:, ((ncol/2) + 1):ncol);
if sum_Bank5 > vref
Bank7= Bank5(:,1:ncol/2);
Bank8= Bank5(:, ((ncol/2) + 1):ncol);

Answers (0)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!