Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
help with m-file

Subject: help with m-file

From: Alex

Date: 14 Apr, 2009 13:50:18

Message: 1 of 5

Hi im getting an error for the following but i dont know what im doing wrong

d = 5
b = 1 0 0 1 1
m-file:
for n = 1:d
    if(b(n) = 1) error on this line
        X(n) = 1
    else
        X(n) = -1
    end
end

Also if I want to do the sum of X from 1 to d how would I do this

Thanks

Subject: help with m-file

From: TravisC

Date: 14 Apr, 2009 14:11:21

Message: 2 of 5

For logical comparisons, it should be == instead of =
 if(b(n) == 1)
Also, for your last question, use the sum command and indexing "sum(X(1:d))". Type "doc sum" at the command line to make sure you know how to use it. Also, read some of the help files on the colon operator and array and logical indexing. For your code below, there are much faster ways to arrive at the same result using vectorization techniques and logical indexing. It doesn't really matter if d equals 5, but when you get into the thousands and beyond it makes a substantial difference.
Hope this helps,
-Travis

"Alex" <a.shan@hotmail.co.uk> wrote in message <gs24aq$3cu$1@fred.mathworks.com>...
> Hi im getting an error for the following but i dont know what im doing wrong
>
> d = 5
> b = 1 0 0 1 1
> m-file:
> for n = 1:d
> if(b(n) = 1) error on this line
> X(n) = 1
> else
> X(n) = -1
> end
> end
>
> Also if I want to do the sum of X from 1 to d how would I do this
>
> Thanks

Subject: help with m-file

From: someone

Date: 14 Apr, 2009 14:11:22

Message: 3 of 5

"Alex" <a.shan@hotmail.co.uk> wrote in message <gs24aq$3cu$1@fred.mathworks.com>...
> Hi im getting an error for the following but i dont know what im doing wrong
>
> d = 5
> b = 1 0 0 1 1
> m-file:
> for n = 1:d
> if(b(n) = 1) error on this line
> X(n) = 1
> else
> X(n) = -1
> end
> end
>

% change:
if(b(n) = 1)
% to:
if(b(n) == 1)
% In the future, showing the actual error message will help.

> Also if I want to do the sum of X from 1 to d how would I do this

doc sum
doc cumsum

>
> Thanks

Subject: help with m-file

From: Gavrilo Bozovic

Date: 14 Apr, 2009 14:13:21

Message: 4 of 5

"Alex" <a.shan@hotmail.co.uk> wrote in message <gs24aq$3cu$1@fred.mathworks.com>...
> Hi im getting an error for the following but i dont know what im doing wrong
>
> d = 5
> b = 1 0 0 1 1
> m-file:
> for n = 1:d
> if(b(n) = 1) error on this line
> X(n) = 1
> else
> X(n) = -1
> end
> end
>
> Also if I want to do the sum of X from 1 to d how would I do this
>
> Thanks

= is for an evaluation, == for a test, so it's:

if b(n) == 1

and not

if b(n) = 1

By a wild turn of events, the function used to do a sum is called "sum".

Try: sum(X(1:d))

Subject: help with m-file

From: Alex

Date: 14 Apr, 2009 14:25:23

Message: 5 of 5

Ok, thanks for the fast response, you have been really helpful

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us