acces indexes of Multidimensional arrays while using index of elements in a vector?

조회 수: 1 (최근 30일)
i have a vector called L the vector contain indexes
also i need to use those indexes while writing the totl matrix MAT(L) into a multidimantional array by doing this:
all this is inside a loop
for k=1:anynumber
MAT(:,:,k)
i found that the script doesn't write at all to the destination matrics it only write to the index (:,:,1) and the rest are zeros.
H(:,:,length(frequencies))=zeros(max(rx_pos),max(tx_pos));
LinInd=sub2ind(size(H(:,:,length(frequencies))),rx_pos,tx_pos);
H(LinInd)=Escat;
H(:,:,k)=H(LinInd);

채택된 답변

dpb
dpb 2014년 9월 17일
편집: dpb 2014년 9월 17일
...the script ... only write[s] to the index (:,:,1)
Yes, because your index-computing expression only addresses a plane in the 3D array, not the whole array. In the expression size(H(:,:,length(frequencies))), the answer is the x,y dimension of a plane so you compute the linear address in the plane, not in the array.
Use
LinInd=sub2ind(size(H),rx_pos,tx_pos,k);
instead for each plane k and then
H(LinInd)=Escat;
where
length(Escat)==length(LinEnd)
You don't need (or want) the subsequent H(:,:,k) assignment at all--that's what the linear addresses will take care of when you convert them to 3D indexing.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by