-3

I need help converting this switch statement from Matlab to something equivalent to Python. Is there a simple way to do it like an if statement?

switch nmax
    case 0
        Tk( 1, : ) = ones( 1, length( x ) )/sqrt(N);
    case 1
        Tk( 2, : ) = (w ./ w1).*ones( 1, length( x ) )/sqrt(N);
    otherwise
        Tk( 1, : ) = ones( 1, length( x ) )/sqrt(N);
        Tk( 2, : ) = (w ./ w1).*Tk( 1, : );

        for m = 3:nmax
            ni=m-1;
            w2_A = N^2-ni^2;
            w2_B = (2*ni+1)*(2*ni-1);
            w2   = ni*sqrt(w2_A/w2_B);
            Tk(m,:) = w./w2.*Tk(m-1,:) - w1/w2*Tk(m-2,:);
        
            w1 = w2;
            T = Tk(m,:);
            for k=0:ni
                Tk(m,:) =  Tk(m,:) - sum(T.*Tk(k+1,:))*Tk(k+1,:);
            end
            h=sqrt(sum(Tk(m,:).^2));
            Tk(m,:) = Tk(m,:)/h;
        end
  • Use an `if` statement. – Scott Hunter Feb 09 '21 at 17:02
  • Welcome to Stack Overflow! Please take the [tour], and read [ask] and [what's on-topic here](/help/on-topic). [Asking on Stack Overflow is not a substitute for doing your own research.](//meta.stackoverflow.com/a/261593/843953) You can find a ton of good resources on the internet, and possibly many questions on SO that deal with similar problems. – Pranav Hosangadi Feb 09 '21 at 17:05
  • Stack Overflow is not intended to replace existing documentation and tutorials. We expect you to do appropriate research before posting a question. This is a trivial browser search. – Prune Feb 09 '21 at 17:13

1 Answers1

1

Python has no switch statement so you have to use if/else

if nmax == 0: # this is equal to your case 0
    # do something
elif nmax == 1: # this is equal to your case 1
    # do something
else: # this is equal to your otherwise case
    # do something
Pranav Hosangadi
  • 23,755
  • 7
  • 44
  • 70
  • thanks. This is what i used but i was not sure if it would work exactly the same as some research that i did, did not clarify it fully – Kostas Keremis Feb 09 '21 at 17:20