Filter löschen
Filter löschen

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

2 Ansichten (letzte 30 Tage)
want2know
want2know am 17 Feb. 2014
Kommentiert: want2know am 18 Feb. 2014
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);

Antworten (0)

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by