Lasse
Lasse

Reputation: 47

Unexpected MATLAB operator in very simple MatlabR2011b script

Very simple tutorial-script, that doesn't run on my MATLAB:

function y=test1(A)
[m,n]=size(A);
y = 0;
for i=1:m
    for j:n
        y=y+A(i,j);
    end
end

Saved in test1.m. When I call it with

A = [ 1 2; 3 4];
s = test1(A)

It gives me:

Error: File: test1.m Line: 5 Column: 10 Unexpected MATLAB operator.

reffering to the n, which MATLAB also in line 5 points out as:

Parse Error at ':': might be invalid MATLAB syntax. In line two it also gives notice, that n might be unused..

How do I make sense of this, syntax looks correct to me?!

Upvotes: 1

Views: 2906

Answers (2)

Roel
Roel

Reputation: 31

It works when you edit line 5 to: for j=1:n

Upvotes: 3

Dan
Dan

Reputation: 45752

for j:n is bad syntax, you probably want for j = 1:n

Upvotes: 4

Related Questions