Indexing an 6 Dimensional array inside a for loop

조회 수: 4 (최근 30일)
bugatti79
bugatti79 2013년 10월 20일
댓글: bugatti79 2013년 10월 21일
Folks,
I am trying to call in the nth page of a 6D array from page 1 to 6. I am using the following code which is not looping through the 6 pages. nu is a 6D array.
for k=1:6;
for ii=1:size(nu,k);
i=nu(ii);
Calcs{ii}= A.*i ...etc
end
end
I think my ii statement is not correct to index the nth page of 6D array. Any ideas? Thanks in advanced B
  댓글 수: 2
dpb
dpb 2013년 10월 20일
No, you're returning the size of the array nu in each dimension in turn.
Unless nu is a cell array, the dimensions in each direction are fixed; Matlab otherwise doesn't support jagged arrays.
The data for a given value of the sixth dimension of a 6D array would be a 5D array and addressed as
d5=nu(:,:,:,:,:,ii); % the ii-th 6th-dimension of 6D array nu
You need to specify precisely which subsection you're intending to operate over.
bugatti79
bugatti79 2013년 10월 21일
Hi dpb,
Thanks for your input. I will note this see how it works. B

댓글을 달려면 로그인하십시오.

답변 (1개)

David
David 2013년 10월 20일
편집: David 2013년 10월 20일
"Folks,
I am trying to call in the nth page of a 6D array from page 1 to 6. I am using the following code which is not looping through the 6 pages. nu is a 6D array.
for k=1:6;
for ii=1:size(nu,k);
i=nu(ii);
Calcs{ii}= A.*i ...etc
end
end
I think my ii statement is not correct to index the nth page of 6D array. Any ideas? Thanks in advanced B"
I assume you are trying to loop through each of the elements along each dimension in the 6D array. If I am correct in my assumption, then I think the problem is as you say. Since nu is a 6D array, you must specify the dimension that is being indexed. In the line you correctly identified as the source of trouble, you are treating nu as if it were only a 1D array. For this to work, you could try reshaping nu into a 2D array, where the elements of each page span their own column. See the code below.
NumDims = ndims(nu);
numPages = size(nu,NumDims);
%Initialize number of elements
numpageElements = 1;
for d=1:NumDims-1
numpageElements = size(nu,d)*numpageElements;
end
%Reshape nu into a N x 6 column matrix.
nunu = reshape(nu,numpageElements,numPages);
and then
%Index through each page
for p=1:6
%Index through the elements on each page
for ii=1:size(:,p)
i = nunu(ii,p);
Calcs{ii} = A.*i ...etc
end
end
You can find more information relevant to your question here: http://www.mathworks.com/help/matlab/math/multidimensional-arrays.html#f1-86846
  댓글 수: 1
bugatti79
bugatti79 2013년 10월 21일
Hi David,
I have only seen your response now, thanks.
I forgot to mention that each page is an 18*9 matrix and 6 of these. Page 1 matrix, all the elements are the same numeric value. Page 2 matrix, all elements are of same value but different to page 1 and so on to page 6.
I am just repeating the set of calculations 6 times. I will look at the code later. Thanks for your help... B

댓글을 달려면 로그인하십시오.

카테고리

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