0

I have 3 vectors x,y and z. How can I create an array a so that a(i,j,k) = [x(i),y(j),z(k)]?

I tried

a(:,:,1) = [[1,2];[3,4]]

but got

Subscripted assignment dimension mismatch.

edit after comment

Basically I first wanted to create an array a from x,y,z, but here is my original problem (so I think creating a vector would be better): I have three vectors: x,y,z, say x = [1 2 3], y = [4,3,2] z = [1,3,2]

I want to compute the gaussian probability function on each [x_i,y_j,z_k] I was thinking using an array. So I first tried using mvnpdf with an array a:

a = rand(3,3,3) < 0.1

and mvnpdf(a,[1,2,3],eye(3))

But matlab returned the following error:

Error using mvnpdf (line 46)
X must be a matrix.

Apparently, he is not happy with me using arrays.

Do you have any idea how I could compute what I want to (apart from using for loops that would take far too long for the size of my real vectors x,y,z).

5
  • 3
    You want a(i,j,k) to be a vector? Commented Oct 28, 2013 at 22:26
  • Doesn't a = [x(:) y(:) z(:)] do what you want? Seems mvnpdf(a,...) needs the first input argument a to be N-by-D. Commented Oct 28, 2013 at 22:37
  • @chappjc not really because I want to compute the mvnpdf for all the combinations x(i),y(j),z(k) (I think your solution would give me only the mvnpdf for x(i),y(i),z(i) right? Commented Oct 28, 2013 at 22:43
  • 1
    Yes, I see now. Luis' answer looks good for all combinations. Commented Oct 28, 2013 at 22:44
  • @teaLeef - It would be helpful if you explicitly stated the order in which you would like x,y,z to vary in the output N-by-D matrix. Commented Oct 28, 2013 at 23:09

1 Answer 1

2

You can do something like this:

x = [10 20 30 40 50]; % example x
y = [100 200 300 400]; % example y
z = [1000 2000 3000]; % example z.    

[ii jj kk] = ndgrid(1:length(x), 1:length(y), 1:length(z));
a = cat(4, x(ii), y(jj), z(kk));

The resulting a is a 4-D array of size length(x)xlength(y)xlength(z)x3. The fourth dimension refers to the three elements of the desired vector. So your desired result [x(i),y(j),z(k)] would be given by a(i,j,k,:), or rather squeeze(a(i,j,k,:)).'. For example,

>> squeeze(a(1,2,3,:)).'
ans =
          10         200        3000

Following your comments, and with thanks to @chappjc: If you want all those vectors piled up as rows of a three-column matrix in lexicographical order, just use:

[kk jj ii] = ndgrid(1:length(z), 1:length(y), 1:length(x));
b = [x(ii(:)).' y(jj(:)).' z(kk(:)).'];

Result:

>> b(1,:)
ans =
          10         100        1000

>> b(2,:)
ans =
          10         100        2000

etc.

Sign up to request clarification or add additional context in comments.

13 Comments

a(1,2,3,:) is still a 4-D array. Is there anyway to get a vector instead? (because of the error I get from mvnpdf when I use an array)? Thanks
Yes, see my answer. squeeze(a(1,2,3,:)) is a column vector, and squeeze(a(1,2,3,:)).' is a row vector.
@teaLeef Note that using meshgrid instead of ndgrid would give wrong results. For instance, with my example data squeeze(a(2,4,3,:)) would give 40, 200, 3000, which is wrong. So ndgrid is the way to go.
@teaLeef I see now. Try b = [x(ii(:)) y(jj(:)) z(kk(:))];. Check the resulting order
@teaLeef - If you decide you need it a different way, you can always change the order by reordering the inputs/outputs of ndgrid, keeping in mind that the first in/out pair changes fastest, then the second, etc. This is why ndgrid is more intuitive than meshgrid IMO. That's all from me, promise!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.