2014-05-23 2 views
1

1 * 262144 행렬을 512 * 512 행렬로 재구성했습니다. 이제 제 2 행렬에서 특정 요소가 필요하며 원래 행 행렬에 있던대로 해당 위치를 알고 싶습니다. 말하자면, 나는 모양이 바뀐 매트릭스에서 (256,4)에있는 요소가 필요합니다. 원래 행의 행렬에서이 요소의 위치를 ​​어떻게 알 수 있습니까?Matlab에서 변형 된 후 원본 행렬 w.r.t를 찾습니다.

matri_working_now = C(1,:); 
    matrix_working_now = reshape(matri_working_now,512,512); 
    [nrows,ncols] = size(matrix_stables); %matrix_stables is a matrix over which I am looping over which contains the locations of the desired elements as per the reshaped matrix. this itself is a 30839*2 matrix 
    for row = 1:nrows 
     for col = 1:ncols 
     %sub2ind(size(matrix_working_now),row,col) 
     %fprintf('iteration is equal to %6.2f.\n',row,col); 
     [rowss colum] = ind2sub(size(matri_working_now),sub2ind(size(matrix_working_now),matrix_stable(row),matrix_stable(col))); % i am accessing the elements of matrix_stables which provide me the row and column numbers; 

     end 
    end 

의견이나 제안이 있으십니까?

감사합니다. 원래 행렬은 벡터이기 때문에

답변

1

, 당신은 단지 sub2ind으로, convert from subindices to linear index 필요 : 일반적으로

col = sub2ind(size(reshapedMatrix), 256,4); 

를 원래 매트릭스는 반드시 벡터가 아닌 경우, 당신은 ind2sub와 두 번째 단계가 필요합니다

[row col] = ind2sub(size(originalMatrix), sub2ind(size(reshapedMatrix), 256,4)); 

예 :

>> originalMatrix = (1:10).^2 
originalMatrix = 
    1  4  9 16 25 36 49 64 81 100 

>> reshapedMatrix = reshape(originalMatrix, 2,5) 
reshapedMatrix = 
    1  9 25 49 81 
    4 16 36 64 100 

>> reshapedMatrix(2,3) 
ans = 
    36 

>> [row col] = ind2sub(size(originalMatrix), sub2ind(size(reshapedMatrix), 2,3)) 
row = 
    1 
col = 
    6 

>> originalMatrix(row,col) 
ans = 
    36 
+0

"범위 밖의 첨자 오류"가 표시됩니다. 더 나은보기를 위해 전체 코드를 다시 게시합니다. –

+0

원래 게시물의 수정 된 코드를 참조하십시오. 감사합니다. –

+0

@KashifNawaz 당신의'sub2ind' 라인에, 당신은 크기가 바뀌 었다고 생각합니다. 내 대답 –

관련 문제