필터 지우기
필터 지우기

Multidimensional array, extracting the Nth dimension

조회 수: 41 (최근 30일)
David
David 2013년 6월 20일
Hi all
I have an array of doubles A which has size [6,8,88].
I want to plot the vector A[1,1,:], but I get the following error:
Error using plot. Data may not have more than 2 Dimensions.
I thought that the operation A[1,1,:] would return a simple vector of length 88, as suggested by the tutorial page here
but it actually has size [1,1,88], which is causing the error.
I've thought of using a loop to extract each element, but this doesn't suit my purpose very well, as I would also like to plot A[i,j,:] for arbitrary i,j, and also perform other operations on these vectors.
Is there a simple way to access the vector A[i,j,:], and have it return a true vector?
I've reproduced the problem with a simpler example, which gives the same error.
A = zeros(3,3,4)
plot(1:4, A(1,1,:))
Sorry for simple question, I'm fairly new at this.
Many thanks for your help
Dave

채택된 답변

Iain
Iain 2013년 6월 20일
plot(1:4, squeeze(A(1,1,:)))
You might need to transpose the squeezed A, I can't remember :P
  댓글 수: 2
David
David 2013년 6월 20일
This solutions seems to suit my purpose best, many thanks for very fast reply. This forum is amazing!
the cyclist
the cyclist 2013년 6월 20일
A big part of getting a fast, useful reply was that you formulated your question clearly and specifically.

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

추가 답변 (2개)

the cyclist
the cyclist 2013년 6월 20일
You could use the permute command:
A_plot = permute(A(1,1,:),[3 1 2]);
to shuffle the 3rd dimension to become the first dimension.

the cyclist
the cyclist 2013년 6월 20일
You could use the reshape command:
A_plot = reshape(A(1,1,:),88,1);
to create reshape to 88x1. More robust, in case the 3rd dimension is not always 88 in length, would be
A_plot = reshape(A(1,1,:),[],1);
which infers the length of the first dimension from the number of elements.

카테고리

Help CenterFile Exchange에서 Line Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by