index must be a positive integer or logical
1 view (last 30 days)
Show older comments
I have this simple code I'm trying to run but I keep getting the error 'index must be a positive integer or logical.' Basically I'm attempting to make a 32x32 image of the f(x,y) bounded by x=0, x=2 and y=0, y=2
a=1;
b=1;
% dbstop if error
for x=0:.0645:2;
for y=0:.0645:2;
f(x,y)=1+sin(2*pi*(a.*x + b.*y));
end
end
imshow(f)
2 Comments
Image Analyst
on 23 Aug 2014
I fixed the formatting for you but read this: http://www.mathworks.com/matlabcentral/answers/13205-tutorial-how-to-format-your-question-with-markup
Accepted Answer
Image Analyst
on 23 Aug 2014
Edited: Image Analyst
on 23 Aug 2014
Indexes can't start at zero like it said, but like you tried to do. To fix it, if you want to use a for loop, do this:
a=1;
b=1;
% dbstop if error
x=0:.0645:2;
y=0:.0645:2;
for column = 1 : length(x)
for row = 1 : length(y)
f(row, column)=1+sin(2*pi*(a.*x(column) + b.*y(row)));
end
end
imshow(f, [], 'InitialMagnification', 600);
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
More Answers (0)
See Also
Categories
Find more on Function Creation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!