???index exceeds matrix dimensions

in my program i have used for loop and if loop at the end i am getting ???index exceeds matrix dimensions this comment after calculating 1 value, where i have to calculate 42 values i used
example
for x=1:42
if x==1
a=1;
b=2;
end
if x==2
a=2;
b=3;
end
:
:
:
:
:
:
if x==42
a=42;
b=43;
end
c=a+b
end

 Accepted Answer

x=1:42
c = 2*x+1
more :)
c = zeros(1,42);
for x = 1:42
a = x;
b = a + 1;
c(x) = a + b;
end
more 2
x=1:42
c = zeros(size(x));
for i1 = 1:numel(x)
if x(i1) == i1
a = x(i1);
b = a + 1;
end
c(i1) = a + b;
end

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!