2
votes

If I have a 3D matrix, X that is 4 x 10 x 50.

The matrix consists of positions and velocities in the first dimension, different particles (or boats or whatever) indexes in the second and lastly the different time steps for the particles movement in the third. Maybe not that important but maybe it clarifies my problem.

Say I want to plot the values of X for specific indices in the first two dimensions across the 3rd dimension

>> plot(X(1,1,:))
Error using plot
Data may not have more than 2 dimensions

Even though the values supplied are one dimensional I cant use plot here because they are given separately like this:

>> X(1,1,1:5)

ans(:,:,1) =
10

ans(:,:,2) =
11.4426

ans(:,:,3) =
12.5169

ans(:,:,4) =
13.7492

ans(:,:,5) =
14.9430

How can I convert the result of X( 1, 1, :) into a vector?

2
class(X) gives the answer doublewhile

2 Answers

2
votes

Indexing into X with X( 1, 1, : ) returns a 3D matrix. However, plot requires its input to be a vector or 2D matrix. To convert X( 1, 1, : ) to vector you need to remove the singleton dimensions. The builtin function squeeze does this:

Try:

X2 = squeeze( X( 1, 1, : ) );
plot( X2 )
1
votes

The way you are indexing it actually yields a 2-d vector. So size(A(:,:,1) is actually 4x10.

To plot it, use Matlab's squeeze operator

plot(squeeze(X(:,:,1))