Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

Problem in using loops - how to have index inside a loop

조회 수: 1 (최근 30일)
want2know
want2know 2014년 2월 18일
마감: MATLAB Answer Bot 2021년 8월 20일
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);

답변 (0개)

이 질문은 마감되었습니다.

태그

Community Treasure Hunt

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

Start Hunting!

Translated by