Page 1 of 1
did I just get stuck in an endless loop?
Posted: Tue Nov 08, 2005 4:52 pm
by ccb056
writing code for an engineering class, taking forever to process
matlab
Code: Select all
clc
clear all
%INPUT SECTION
fprintf('The files selected below must have the same dimensions.\n\n')
s=1;
e=0;
while s > e
p = input('prefix: ','s');
s = input('start file number: ');
e = input('end file number: ');
t = input('file extension: ','s');
if s > e
fprintf('start file number must be < end file number')
end
end
%CALCULATION SECTION
n = abs(e-s+1);
sn = num2str(s);
name = [p sn t];
x = imread(name);
sizex = size(x);
%HEIGHTS
heightl = sizex(1);
oheight = heightl;
%WIDTHS
widthl = sizex(2);
owidth = n*widthl;
%INDEX
index=owidth/widthl;
fa = zeros(oheight,owidth);
for s = s:1:e
sn = num2str(s);
name = [p sn t];
x = imread(name);
for ox = 1:1:owidth
for oy = 1:1:oheight
for lx = (s*widthl-widthl+1):1:s*widthl
for ly = 1:1:heightl
fa(oy,ox) = x(ly,lx);
end
end
end
end
end
%OUTPUT SECTION
Posted: Tue Nov 08, 2005 5:15 pm
by CDN_Merlin
s=1;
e=0;
while s > e
p = input('prefix: ','s');
s = input('start file number: ');
e = input('end file number: ');
t = input('file extension: ','s');
if s > e
fprintf('start file number must be < end file number')
end
end
Should it be "if e > s" ?? Or if it's not, then maybe you need to set S=0 and e=1?
Not sure I'm not a programmer
Posted: Tue Nov 08, 2005 5:18 pm
by Iceman
Posted: Tue Nov 08, 2005 5:25 pm
by Krom
Merlin, it's coded so it doesn't have to stop at 0, it could stop at 12, or 2000, etc.
I think a problem would be here: "while s > e" is the start of a loop, every time it goes through the loop it will test to see if s > e, so if you don't want it to loop forever you have to put s-- in there at some point.
Posted: Tue Nov 08, 2005 5:30 pm
by Lothar
Krom wrote:if you don't want it to loop forever you have to put s-- in there at some point
No.
That loop is taking user input for s and e, every time through. It's basically just waiting for the user to input s and e values that are correct in relation to each other. If the user is doing the right thing, that loop should terminate in one single iteration.
ccb, where is the program hanging up? Try putting some print statements every so often so that you can tell how far the code is getting before it freezes. The more information you can give us, the better...
Posted: Tue Nov 08, 2005 5:38 pm
by Krom
And just for some added useful help, make your print statements list the values of all the variables in use.
Posted: Tue Nov 08, 2005 5:56 pm
by ccb056
the code is hanging up here:
Code: Select all
for s = s:1:e
sn = num2str(s);
name = [p sn t];
x = imread(name);
for ox = 1:1:owidth
for oy = 1:1:oheight
for lx = (s*widthl-widthl+1):1:s*widthl
for ly = 1:1:heightl
fa(oy,ox) = x(ly,lx);
end
end
end
end
end
Posted: Tue Nov 08, 2005 6:02 pm
by ccb056
just an overview of what the program is supposed to do
the user has 36 files, they are named file1.bmp to file36.bmp
each bmp is a black/white bmp
the script, loads each bmp as a matrix, each cell in the matrix represents a pixel of the bmp, black is 0 and white is 255
it then, combines all the bmp matrixes into one big matrix
each bmp matrix has the exact same dimensions
the program is taking forever to combine all the bmp matrixes into one big matrix, either I have an infinite loop programed, or its a dog to process, ive got a dualie 1800 with a gig of ram, so im pretty well covered when it comes to hardware, the code has been running for 30 minutes...
Posted: Tue Nov 08, 2005 6:17 pm
by ccb056
bah, it just finished, and gave me this error
The files selected below must have the same dimensions.
prefix: labdoc_
start file number: 1
end file number: 36
file extension: .bmp
??? Attempted to access x(1,35); index out of bounds because size(x)=[1584,34].
Error in ==> hmwk11_6 at 56
fa(oy,ox) = x(ly,lx);
Posted: Tue Nov 08, 2005 8:45 pm
by will_kill
Personally I think if you took a job that you can't complete your a moron and you should hand it over to someone who can complete it...c'mon, be a man about it.
ok...were even now
Posted: Tue Nov 08, 2005 9:03 pm
by ccb056
hah, ok
Posted: Tue Nov 08, 2005 10:14 pm
by Grendel
Of to the Coders Corner you go..
Posted: Tue Nov 08, 2005 10:16 pm
by ccb056
bah, i put it in the tech forum because no one visits the coders corner, at 1024 * 768 its below the fold
Posted: Tue Nov 08, 2005 10:35 pm
by DCrazy
ccb: coders visit the Coders' Corner.
What language is this? Do array indices start at 0? If so, you need to account for that in your for loops.
Posted: Tue Nov 08, 2005 10:37 pm
by ccb056
ok, so, want to shed some divine insight coder?
language is matlab, matrixes start at 1, unlike c++ where they start at 0
i actually ran the code, its not an endless loop, the code did finish after about an hour, the problem is it gave me an array dimension error
each array it loads is about 30 * 1500 it loads 36 of these, it then copies these to a big array that is originally filled with 0s
Posted: Wed Nov 09, 2005 1:51 am
by Sirius
To cut the runtime down, you can either
1) use a faster programming language (may not be an option, but MATLAB is generally pretty slow being interpreted)
2) (better) reduce the complexity of that loop. With 5 or 6 nested for loops, I'm not surprised it takes so long.
It's more difficult to tell what causes the error, especially since it's been some time since I used MATLAB at all...
Posted: Wed Nov 09, 2005 6:31 am
by ccb056
got it
Code: Select all
clc
clear all
%INPUT SECTION
fprintf('The files selected below must have the same dimensions.\n\n')
s=1;
e=0;
while s > e
p = input('prefix: ','s');
s = input('start file number: ');
e = input('end file number: ');
t = input('file extension: ','s');
if s > e
fprintf('start file number must be < end file number')
end
end
%CALCULATION SECTION
n = abs(e-s+1);
sn = num2str(s);
name = [p sn t];
x = imread(name);
sizex = size(x);
%HEIGHTS
heightl = sizex(1);
oheight = heightl;
%WIDTHS
widthl = sizex(2);
owidth = n*widthl;
%INDEX
index=owidth/widthl;
fa = zeros(oheight,owidth);
for i = s:1:e
sn = num2str(i);
name = [p sn t];
x = imread(name);
for lx = 1:1:widthl
for ly = 1:1:heightl
oy = ly;
ox = lx + (i - s)*widthl;
fa(oy,ox) = x(ly,lx);
end
end
end
%OUTPUT SECTION
Posted: Wed Nov 09, 2005 11:56 am
by fliptw
Unless matlab does some freaky ★■◆● with array indices this:
is pure evile, as floats aren't good integers.
Posted: Wed Nov 09, 2005 1:31 pm
by Sirius
Unless owidth is an exact multiple of widthl, which wouldn't surprise me. And if MATLAB had automatic downcasting ... which, theoretically, is a bad idea, but anyway.
Code looks much nicer now. Something more like O(w * h * n) instead of O(w^2 * h^2 * n)...
Posted: Wed Nov 16, 2005 4:04 am
by SirWinner
Flip,
Your comment above reminds me of a mistake that a rookie programmer made. Used a Floating point variable then tried to use it where an Integer value was needed.
When they printed the value of the variable it looked like an integer but with a very minor amount of looking at the code, I found the problem very quickly.
The result of their error in coding was that the value was getting truncated to the integer value which was one less than what they expected. So they ended up with the wrong result in their code.
This was a quick lesson to them to use the proper variable types instead of assuming that a floating point number is exchangable directly with an integer value.