2011-11-27 2 views
0

대신 배열에 0을 지정하여 범위를 벗어나는 배열 액세스를 자동 처리하고 싶습니다.MATLAB autohandle 배열은 0을 반환합니다.

그러나 내가 지금 가지고있는 것은 s 배열입니다

evenIndexNext = 2*j+1 + 2*i ; 
oddIndexPrev = 2*j+1 - i ; 
evenValueNext = 0 ; 
oddValuePrev = 0 ; 
if(evenIndexNext <= n) 
    evenValueNext = s(evenIndexNext) ; 
end 
if(oddIndexPrev >= 1) 
    oddValuePrev = s(oddIndexPrev) ; 
end 

과 같은 코드입니다. 조금 어색해.

try 
    evenValueNext = s(evenIndexNext) ; 
catch 
    evenValueNext=0; 
end 

또는, 당신은 그렇게 할 수있는 기능을 정의 할 수 있습니다 :

답변

2

아마, 당신은 할 수

function y=checkBound(l,i) 
    if (i<1) || (i>numel(l)) 
    y=0; 
    else 
    y=l(i); 
    end 
end 

evenValueNext = checkBound(s,evenIndexNext); 
oddValuePrev = checkBound(s,oddIndexPrev) ; 
+0

나는 우리가 할 수있는 최선의 기능을 추측한다. – bobobobo

0

이 작업을 처리 할 수있는 새로운 클래스를 정의 할 수 있습니다. 클래스의 subsref 메소드가 오버로드 된 경우 인덱스가 범위를 벗어 났는지 여부를 확인할 수 있고 그럴 경우 0을 반환합니다.

관련 문제