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