did I just get stuck in an endless loop?

For all coding issues - MODers and programmers, HTML and more.

Moderators: Jeff250, fliptw

Post Reply
User avatar
ccb056
DBB Fleet Admiral
DBB Fleet Admiral
Posts: 2540
Joined: Wed Jul 31, 2002 2:01 am
Contact:

did I just get stuck in an endless loop?

Post 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
User avatar
CDN_Merlin
DBB_Master
DBB_Master
Posts: 9782
Joined: Thu Nov 05, 1998 12:01 pm
Location: Capital Of Canada

Post 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
User avatar
Iceman
DBB Habitual Type Killer
DBB Habitual Type Killer
Posts: 4929
Joined: Thu Apr 20, 2000 2:01 am
Location: Huntsville, AL. USA
Contact:

Post by Iceman »

FYI: We have a Coder's Corner forum
User avatar
Krom
DBB Database Master
DBB Database Master
Posts: 16140
Joined: Sun Nov 29, 1998 3:01 am
Location: Camping the energy center. BTW, did you know you can have up to 100 characters in this location box?
Contact:

Post 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.
User avatar
Lothar
DBB Ghost Admin
DBB Ghost Admin
Posts: 12133
Joined: Thu Nov 05, 1998 12:01 pm
Location: I'm so glad to be home
Contact:

Post 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...
User avatar
Krom
DBB Database Master
DBB Database Master
Posts: 16140
Joined: Sun Nov 29, 1998 3:01 am
Location: Camping the energy center. BTW, did you know you can have up to 100 characters in this location box?
Contact:

Post by Krom »

And just for some added useful help, make your print statements list the values of all the variables in use.
User avatar
ccb056
DBB Fleet Admiral
DBB Fleet Admiral
Posts: 2540
Joined: Wed Jul 31, 2002 2:01 am
Contact:

Post 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
User avatar
ccb056
DBB Fleet Admiral
DBB Fleet Admiral
Posts: 2540
Joined: Wed Jul 31, 2002 2:01 am
Contact:

Post 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...
User avatar
ccb056
DBB Fleet Admiral
DBB Fleet Admiral
Posts: 2540
Joined: Wed Jul 31, 2002 2:01 am
Contact:

Post 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);
User avatar
will_kill
DBB Captain
DBB Captain
Posts: 721
Joined: Mon Jul 11, 2005 5:52 pm

Post 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 :wink: :lol:
User avatar
ccb056
DBB Fleet Admiral
DBB Fleet Admiral
Posts: 2540
Joined: Wed Jul 31, 2002 2:01 am
Contact:

Post by ccb056 »

hah, ok ;)
User avatar
Grendel
3d Pro Master
3d Pro Master
Posts: 4390
Joined: Mon Oct 28, 2002 3:01 am
Location: Corvallis OR, USA

Post by Grendel »

Of to the Coders Corner you go.. :)
User avatar
ccb056
DBB Fleet Admiral
DBB Fleet Admiral
Posts: 2540
Joined: Wed Jul 31, 2002 2:01 am
Contact:

Post by ccb056 »

bah, i put it in the tech forum because no one visits the coders corner, at 1024 * 768 its below the fold
User avatar
DCrazy
DBB Alumni
DBB Alumni
Posts: 8826
Joined: Wed Mar 15, 2000 3:01 am
Location: Seattle

Post 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.
User avatar
ccb056
DBB Fleet Admiral
DBB Fleet Admiral
Posts: 2540
Joined: Wed Jul 31, 2002 2:01 am
Contact:

Post 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
User avatar
Sirius
DBB Master
DBB Master
Posts: 5616
Joined: Fri May 28, 1999 2:01 am
Location: Bellevue, WA
Contact:

Post 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...
User avatar
ccb056
DBB Fleet Admiral
DBB Fleet Admiral
Posts: 2540
Joined: Wed Jul 31, 2002 2:01 am
Contact:

Post 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
User avatar
fliptw
DBB DemiGod
DBB DemiGod
Posts: 6459
Joined: Sat Oct 24, 1998 2:01 am
Location: Calgary Alberta Canada

Post by fliptw »

Unless matlab does some freaky ★■◆● with array indices this:

Code: Select all

index=owidth/widthl;
is pure evile, as floats aren't good integers.
User avatar
Sirius
DBB Master
DBB Master
Posts: 5616
Joined: Fri May 28, 1999 2:01 am
Location: Bellevue, WA
Contact:

Post 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)...
User avatar
SirWinner
DBB Fleet Admiral
DBB Fleet Admiral
Posts: 2700
Joined: Thu Nov 05, 1998 12:01 pm
Location: Oklahoma City, Oklahoma, United States of America
Contact:

Post 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.

:P
Post Reply