2011-11-28 115 views
0

我真的是一個在Matlab的noobie,我正在製作一個程序。這個程序只是試圖讀取一個二進制數,例如(00011),前3個字符決定了它將做什麼操作,剩下的就是要進入操作(添加,乘法等)的操作。但是,一個錯誤一直在殺死我,「索引超過矩陣尺寸」,我明白,matlab自動將維度應用於矩陣,並且這使我在我的背上竊聽...其他...索引超過矩陣尺寸

這裏是代碼。

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 

的問題是,在「R」的變量,是在開關時和被選擇的情況下,標記錯誤「指數超過矩陣尺寸」。

任何想法或建議,以解決這個問題?

在此先感謝。

PS:忘了把ihighh代碼和我低...對不起...這裏是....

%%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

「我明白,matlab自動將維度應用於矩陣」......我不確定_I_明白這一點。矩陣_has_維度,它不能做任何事情,並沒有從MATLAB中得到它們。 ;-) –

回答

3

我覺得你的代碼是不完整的。但無論如何,如果您自己調試代碼,它會更有用。

在MATLAB中,這可以通過dbstop if error命令輕鬆完成。然後運行你的程序,每當發生一個未被捕獲的錯誤時它就會切換到調試模式。然後,您應該手動調查r的大小以及您嘗試訪問的索引。這些不匹配,所以您應該確定矩陣是否太小或您的索引是否在錯誤的範圍內並且正確。

要退出調試模式,請輸入命令dbquit並防止MATLAB自動切換到調試模式,只需運行dbclear if error即可。

關於調試器的更多信息可以在MATLAB documentation中找到。

+0

那麼,我申請了你的建議,我得到了下一個: –

+0

我用decPGL(00011),和matlab給了我下一件事.. –

+0

???試圖訪問r(3);指數越界,因爲numel(r)= 1。 錯誤==> decPGL> unPaso at 50 r(i)= r(i)+ r(j); 錯誤在==> decPGL在32 unPaso(programa(ins));我怎樣才能讓變量「r」的大小變大? –