???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

1 件のコメント

Walter Roberson
Walter Roberson 2011 年 12 月 8 日
There is no such thing as an if loop.

サインインしてコメントする。

 採用された回答

Andrei Bobrov
Andrei Bobrov 2011 年 11 月 22 日

0 投票

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by