help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Multiple case function error


From: Harishkumar U
Subject: Re: Multiple case function error
Date: Sat, 4 Apr 2020 22:22:15 +0530

Thanks for the feedback! 

This is what I am trying to achieve, this is a sample test code for a case operation

A = [3 4 5; 7 2 6; 8 1 9];

d = A(1,1);
g = A(2,1);


y = input('enter a value: ');

switch (y)
  case d
    xpos = [A(1,3) A(2,2) A(3,1)]
    xneg = [A(3,1) A(2,2) A(1,3)]
  case g
    xxpos = [A(1,1) A(2,1) A(2,2)]
    xxneg = [A(1,1) A(2,1) A(2,2)]
    endswitch
    z = input('enter a different value: ');
    switch (z)
      case {xpos, xxpos}
        r = A(2,2)+A(2,1)
      case {xneg, xxneg}
        n = A(3,1)+A(2,1)
       
endswitch

After displaying the results of first switch, I would like to use a different switch whose values are in the range xpos or xxpos it should perform one operation then if
the value is in range of xneg or xxneg it should perform another operation. But since we either chose one case the other case value is not invoked to it throws an error like this

enter a different value: 5
error: 'xxpos' undefined near line 19 column 19
error: called from
    nabbleuserJamessherman at line 18 column 5

On Fri, Apr 3, 2020 at 1:30 AM James Sherman Jr. <address@hidden> wrote:

On Thu, Apr 2, 2020 at 2:40 AM Harishkumar U <address@hidden> wrote:
This is what I have done now

x = {d = 3, e = 4, f = 5};
a = {g = 7, h = 2, i = 6};
b = {k = 8, l = 1, m = 9};

d = x(1,1);
e = x(1,2);
f = x(1,3);
g = a(1,1);
h = a(1,2);
i = a(1,3);
k = b(1,1);
i = b(1,2);
m = b(1,3);

y = input('enter a value: ');
switch(y)

case d
  xpos ={d, h, m};
  xneg ={k, h, f};
 
  ss = input('enter cross value: ');
 
case g
  xxpos = {g, h, d};
  xxneg = {f, i, e};
 
  ss = input('enter cross value: ');

endswitch

if (ss = xpos && xxpos)


  max = d+h;
  disp(max)
 
else
 
  min = g-f;
  disp(min)
 
endif

but now, I am getting these errors

enter a value: d
enter cross value: h
error: invalid conversion from cell array to logical value
error: called from
    crossminmax at line 32 column 1

enter a value: d
error: binary operator '==' not implemented for 'cell' by 'scalar' operations
error: called from
    crossminmax at line 16 column 1

On Thu, Apr 2, 2020 at 12:05 PM Harishkumar U <address@hidden> wrote:
Thank you! I changed cross variable name. Yes, switch y block input should match the output of cross block to obtain either xpos or xneg value. But, when I use 'd' its ignoring the case statement and skipping forward. Can I use If else condition? something similar to switch case statement though 

On Thu, Apr 2, 2020 at 2:14 AM Nicholas Jankowski <address@hidden> wrote:


On Wed, Apr 1, 2020 at 1:31 PM harish3679 <address@hidden> wrote:
Hi,

I am trying to use multiple cases for a math operation using case{..,..} as
per octave reference. But unfortunately I am getting an error
'enter a value: d
enter cross value: h
error: 'xxpos' undefined near line 24 column 13
error: called from
    crossminmax at line 22 column 1'


I've had some trouble even trying to recreate your error.  

1 - did you define some values for d, etc, outside of what you showed? because if I run your script from a clean workspace i get:

enter a value: d
error: 'd' undefined near line 1 column 1
error: called from
    casetest at line 3 column 3

i notice some other things.  You use cross as a variable name, but it is the name of a function for performing a vector cross product.  so if the first switch doesn't set a value to overwrite the meaning of cross, you get an error calling the switch(cross) line. 

do you mean for your switch(y) block to match the letter input d or g?  in that case you need it to be defined as a character with quotation marks,  'd', or "d".  Of course, then it's not clear what values will be going into xpos or xxpos.

look at the help for switch with:  >> help switch





--
Harish Kumar

M: +91 9488309620, Whatsapp: +91 9944308505

E: address@hidden

A:

15/36, Ramanna Layout 2, Bharathi Park Road 2, 


Saibaba Colony, Coimbatore -641 043. Tamil Nadu, India







--
Harish Kumar

M: +91 9488309620, Whatsapp: +91 9944308505

E: address@hidden

A:

15/36, Ramanna Layout 2, Bharathi Park Road 2, 


Saibaba Colony, Coimbatore -641 043. Tamil Nadu, India






Hi Harish,

Please use bottom posting in this thread.

The issue you're facing is that you're using a cell array (i.e. xpos is a cell array) in your if statement, so it can't convert a cell array to a logical value (true or false) to decide whether to execute the if or else part of that statement.  I'm really unclear about what the intent of your code is, especially since you redefine variables before doing anything with them.  For example, in your first statement x = ..., what that does is assign the values of d to 3, e to 4, and f to 5, then makes a cell array out of those values. But then, you redefine d on line 5 to be equal to a 1x1 cell array of which contains the value 3. 

Also, in your if statement, you're setting ss equal to the variable xpos?  I don't know what you intend here.  Do you mean to ask if the number the user entered is one of the values in your cell array xpos?  I don't think you're getting anything out of using cell arrays like this.  I'd suggest something more like:

A = [3 4 5; 7 2 6; 8 1 9];

d = A(1,1);
g = A(2,1);

y = input('enter a value: ');

switch (y)
  case d
    xpos = [A(1,3) A(2,2) A(3,1)]
    xneg = [A(3,1) A(2,2) A(1,3)]
  case g
    xxpos = [A(1,1) A(2,1) A(2,2)]
    xxneg = [A(1,1) A(2,1) A(2,2)]
endswitch

Will get you most of the way there, I think.  And, IMO, its a lot more straight forward.

Hope this helps,

James Sherman



--
Harish Kumar

M: +91 9488309620, Whatsapp: +91 9944308505

E: address@hidden

A:

15/36, Ramanna Layout 2, Bharathi Park Road 2, 


Saibaba Colony, Coimbatore -641 043. Tamil Nadu, India






reply via email to

[Prev in Thread] Current Thread [Next in Thread]