I'm really a noobie in Matlab, and i was making a program in it. This program just tries to read a binary number, for example (00011), the first 3 characters dictate what operation it will make, and the rest are the ones that are going to enter the operation (add, multiply, etc.). But an error keeps killing me, the "index exceeds matrix dimensions", i understand that matlab autmatically applies dimensions to a matrix, and this keeps me bugging on my back ...
here's the code.
function res = decPGL(varargin)
persistent rInicial
global r alto
if isempty(varargin)
res = rInicial;
elseif isequal(varargin{1},'r') || isequal(varargin{1},'R')
rInicial = varargin{2};
res = rInicial;
else
alto = 0; % bandera que termina el programa
programa = varargin{1}; % vector del programa completo
ins = 1; % número de instrucción que se va a ejecutar
r = rInicial; % estado inicial de registros
while ins<=length(programa) && ~alto
unPaso(programa(ins));
ins = ins + 1;
end
res = r;
end
function unPaso(segmento)
% executes a segment of the program
global r alto
hh4 = ihighh(segmento,4);
i = ilow(ihighh(segmento,2),2)+1;
j = ilow(segmento,2)+1;
if hh4 <= 5
switch hh4
case 0
r(i) = r(i) + r(j);
case 1
r(i) = r(i) - r(j);
case 2
r(i) = r(i) * r(j);
case 3
if r(j) ~= 0
r(i) = r(i)/r(j);
end
case 4
r(i) = j;
case 5
t = r(i);
r(i) = r(j);
r(j) = t;
end
elseif hh4==6
switch i
case 1
r(j) = exp(r(j));
case 2
r(j) = log(abs(r(j)));
case 3
r(j) = r(j)^2;
case 4
r(j) = sqrt(abs(r(j)));
end
elseif hh4==7
switch i
case 1
r(j) = -r(j);
case 2
r(j) = sin(r(j));
case 3
r(j) = cos(r(j));
case 4
r(1) = r(j);
alto = 1;
end
end
The problem is in the "r" variable, when is in the switch and a case is chosen, marks the error "Index exceeds matrix dimensions".
Any ideas or suggestions to solve this?
Thanks in advance.
PS: Forgot to put the ihighh code and the i low ... sorry ... here it is ....
%%ihigh
function res = ihigh(p, m, varargin)
if length(varargin)>=1
B = varargin{1};
else
B = 2;
end
res = p - mod(p,B.^m);
%%ihighh
function res = ihighh(p, m, varargin)
if length(varargin)>=1
B = varargin{1};
else
B = 2;
end
res = ihigh(p,m,B)./(B.^m);
%%ilow
function res = ilow(p, m, varargin)
if length(varargin)>=1
B = varargin{1};
else
B = 2;
end
res = mod(p,B.^m);