0
votes

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);
1
"i understand that matlab autmatically applies dimensions to a matrix"...I am not sure I understand that. A matrix has dimensions, it cannot do anything about it and does not get them from MATLAB. ;-)arne.b

1 Answers

3
votes

I think your code is incomplete. But anyhow, it is more useful if you debug your code yourself.

In MATLAB this can be done easily by the dbstop if error command. Then run your program and it will switch to debug mode whenever an uncaught error occurs. You should then manually investigate the size of r and what index you try to access. These will not match, so you should determine whether the matrix is too small or your index is in a wrong range and correct for that.

To quit from debug mode, enter the command dbquit and to prevent MATLAB from switching to debug mode automatically, just run dbclear if error.

More information on the debugger can be found in the MATLAB documentation.