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

Answers (4)

Andrei Bobrov
Andrei Bobrov on 3 Nov 2012
Edited: Andrei Bobrov on 3 Nov 2012
m = bsxfun(@(x,y)8*x./abs(3*y-1) - abs(3./y.^2),.85:.05:1.2,(.5:.1:3).');
Azzi Abdelmalek
Azzi Abdelmalek on 3 Nov 2012
Edited: Azzi Abdelmalek on 3 Nov 2012
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 on 3 Nov 2012
Edited: Matt J on 3 Nov 2012
[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);

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 3 Nov 2012

Community Treasure Hunt

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

Start Hunting!