2014-04-26 2 views
0

여러 날짜와 포트폴리오가 포함 된 대용량 데이터 파일의 많은 주식에 대해 시장 가중치를 작성해야하는 MATLAB 코딩 문제에 봉착했습니다.MATLAB 루프 프로그래밍

요즘에는 '중첩 루프'를 사용하여 전문가의 도움을 받았지만 마지막 줄에서 무엇을했는지 이해하지 못합니다. 나는 누군가가 약간의 빛을 비출 수 있었는지 궁금해서 마지막 코딩 라인을 설명했다. XP = X (여기서 x = 시장 가치)을

dates=unique(x(:,1)); (finds the unique dates in the data set Dates are column 1) 
for i=1:size(dates,1) (creates an empty matrix to fill the data in) 
    for j=5:size(xp,2) 
    xp(xp(:,1)==dates(i),j)=xp(xp(:,1)==dates(i),j)./sum(xp(xp(:,1)==dates(i),j)); (help???) 
    end 
end 

모든 의견은 매우 감사합니다!

답변

1

코드를 이해하려면 콜론 연산자, 논리적 색인 및 /./의 차이점을 이해해야합니다. 이 중 하나라도 분명하지 않은 경우 설명서에서 찾아보십시오.

다음 코드는 동일한 않지만, I는 단일 라인으로 각 단계를 구분하기 때문에 쉽게 읽을 :

이 코드
dates=unique(x(:,1)); 
%iterates over all dates. size(dates,1) returns the number of dates 
for i=1:size(dates,1) 
    %iterates over the fifth to last column, which contains the data that will be normalised. 
    for j=5:size(xp,2) 
    %mdate is a logical vector, which is used to select the rows with the currently processed date. 
    mdate=(xp(:,1)==dates(i)) 
    %s is the sums up all values in column j same date 
    s=sum(xp(mdate,j)) 
    %divide all values in column j with the same date by s, which normalises to 1 
    xp(mdate,j)=xp(mdate,j)./s; 
    end 
end 

는 I가 디버거를 사용하여 코드를 통해 단계를 제안한다.