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

1 view (last 30 days)
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);

Accepted Answer

dpb
dpb on 17 Sep 2014
Edited: dpb on 17 Sep 2014
...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.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!