How to efficiently encode two values as a unique entitiy
    4 visualizaciones (últimos 30 días)
  
       Mostrar comentarios más antiguos
    
    balandong
      
 el 23 de Nov. de 2018
  
    
    
    
    
    Respondida: Andrei Bobrov
      
      
 el 23 de Nov. de 2018
            Dear all,
With respect to the above subject, I wonder if we can make the following code more efficient and compact (reduce the ifelse)..
A=3;
B=3;
C=[];
if A==1 && B==1
    C=1;
elseif  A==1 && B==2
    C=2;
    elseif  A==1 && B==3
    C=3;
    elseif  A==2 && B==1
    C=4;
    elseif  A==2 && B==2
    C=5;
    elseif  A==2 && B==3
    C=6;
    elseif  A==3 && B==1
    C=7;
    elseif  A==3 && B==2
    C=8;
    elseif  A==3 && B==3
    C=9;
end
In addition, I also wonder if we can scale it up for A=[2;2;3;1] and B= [1;1;3;1]
Thanks in advance
0 comentarios
Respuesta aceptada
Más respuestas (1)
  Andrei Bobrov
      
      
 el 23 de Nov. de 2018
        in your case: 
>> A=[3;3;3;1]; B= [1;1;3;1];
f = @(A,B)  3*(A(:) - 1) + B(:);
C = f(A,B)
C =
     7
     7
     9
     1
>> 
0 comentarios
Ver también
Categorías
				Más información sobre Whos en Help Center y File Exchange.
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


