Error with noninteger numbers

Problem
I am trying to run the following equation. # 'n' from 0.5 to 3 with step of 0.1 # 'i' from 0.85 to 1 with step 0.05.
After running the M-file. the following error is popping on the screen:
?? Attempted to access m(0.5,0.85); index must be a positive integer or logical.
m = zeros(25,2);
for n = 0.5:0.1:3;
for i = 0.85:0.05:1.2
m(n,i) = ((8*i)/(abs(3*n - 1))) - abs(3/n^2)
end
end
Please help.
Thanks in advance.
Pushkar

回答(4 个)

Andrei Bobrov
Andrei Bobrov 2012-11-3
编辑:Andrei Bobrov 2012-11-3
m = bsxfun(@(x,y)8*x./abs(3*y-1) - abs(3./y.^2),.85:.05:1.2,(.5:.1:3).');
you can't use
x(0.5) or x(-1)
the index must be a positive integer or logical
m = zeros(25,2);
k1=0;
for n = 0.5:0.1:3;
k1=k1+1
k2=0;
for i = 0.85:0.05:1.2
k2=k2+1;
m(k1,k2) = ((8*i)/(abs(3*n - 1))) - abs(3/n^2)
end
end
I suggest this approach:
m = zeros(25,2);
n = 0.5:0.1:3;
i = 0.85:0.05:1.2;
for k1 = 1:length(n)
for k2 = 1:length(i)
m(k1,k2) = ((8*i(k2))./(abs(3*n(k1) - 1))) - abs(3./n(k1)^2);
end
end
Matt J
Matt J 2012-11-3
编辑:Matt J 2012-11-3
[n,i]=ndgrid(0.5:0.1:3, 0.85:0.05:1.2);
m = ((8.*i)./(abs(3.*n - 1))) - abs(3./n.^2);

类别

帮助中心File Exchange 中查找有关 Loops and Conditional Statements 的更多信息

提问:

2012-11-3

Community Treasure Hunt

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

Start Hunting!

Translated by