Performing iteration with 'for loop' in MATLAB

Status
Not open for further replies.

sambezi

Newbie level 4
Joined
Aug 16, 2015
Messages
5
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
42
In the code below, i am using "for loop" to get values of px for each iteration of i. There should just be one value of px per iteration or loop, but i am getting three values instead. What could be the error in my code?

Code:
clear; clc;
for i = 0:1:4;
    for m = 1:1:4;
        if i == 0
            px = 0.5;
        else if i == 2*m-1
                px = 2;
            else if i == 2*m
                    px = 3;
                end
            end 
        end 
        px = px
    end 
    a = px
end
 

What do you mean you are "getting three values"? And I think you might be missing some 'elses'. And what's the px=px for?

Every time through the "i" loop you'll get four (not three) values.
 

It's unclear as to the intended task or purpose of your code.

Perhaps you should elaborate as to the intended goal or outcome.

Are you attempting to create a function which returns a specific value or vector?


BigDog
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…