how can i write a for loop for g=[2 5 6 8 9]

i have g=[2 5 6 8 9]
i want to write a 'for loop' to increment g
g changes with respect to considered system
for i=1:g
for j=1:14
something
end

Answers (1)

Stephan
Stephan on 25 Feb 2019
Edited: Stephan on 25 Feb 2019
Hi,
use numel function:
for i = 1:numel(g)
Best regards
Stephan

5 Comments

Stephan
Stephan on 25 Feb 2019
Edited: Stephan on 25 Feb 2019
BTW: i and j are not recommended as counter variables in for loops - the are reserved for the imaginary part of complex numbers.
thank you Mr.stephan, but it doesnot considering last value of g
what’s your expected output?
g is generator bus data taken from 14bus sytem
in for loop i used power injection equations. my requirement is power injections at generator buses
Stephan
Stephan on 25 Feb 2019
Edited: Stephan on 25 Feb 2019
Can you explain a bit more clear? Usually if a vector changes in length, numel is the right choice. If this is not what you want to do, please explain.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Asked:

on 25 Feb 2019

Edited:

on 25 Feb 2019

Community Treasure Hunt

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

Start Hunting!