1

In MATLAB I have

[Z,S]=meshgrid(0.01:0.01:1)

and I also have a 100000x2 matrix called X, each row has two sets of data - p is the first column and x is the 2nd.

I want to compute exp^(-S*X(j,2)).(*Z.^X(j,1)) where j indexes the row. The result should be a 100x100x100000 matrix. This will then be averaged along the 3rd dimension and a mesh plot will be produced. I've tried using a for loop

[Z,S]=meshgrid(0.01:0.01:1)
for j=1:100000
phi(j)=exp^(-S.*X(j,2)).*(Z.^X(j,1))
end

to produce the 100x100x100000 array I need. However this gives me the error

In an assignment  A(I) = B, the number of elements in B and I must be the same.
Error in phi (line 4)
phi(j)=exp(-S.*X(j,2)).*(Z.^X(j,1));

I'm not sure why this is happening? Can anyone figure out a better way of trying to find the result I want? Because I'm guessing there could be a fully vectorised solution (or least use of for loops)?

3
  • I'm having trouble following your indices - can you say a little bit more about the object you're trying to create? Is this like an 2D exponential with random noise or something? Commented Aug 29, 2015 at 2:05
  • 1
    You are trying to assign a matrix to a single spot in phi. You are obviously getting a dimension mismatch because of that. Commented Aug 29, 2015 at 3:39
  • @anon0909 no that part isn't a 2D exponential - I'm trying to calculate e^(-sp)z^x, where the p and x are random variables stored in the array X. 100000 samples have been taken so that each row of X has a p and x corresponding to one sample. The j in my question simply numbers which particular sample we are looking at. I will then want to average over all the samples. Commented Aug 29, 2015 at 13:08

1 Answer 1

2

Assume that you are using two more nested loops to get Z and S, thus the code would have three nested loops in total.

Now, the vectorizing techniques haven't changed on vectorizable nested loops cases like these - Treat different parts of the code that involve different iterators separately. Thus, here you have three iterators, two of which are 100 in length and the third ones goes until 100000. Putting the vectorizing ideas in a concise commented text and solving your case with bsxfun based code -

%// Get vectorized equivalent of exp(-S.*X(j,2)) and keeping in mind that
%// since the last (3rd) dimension of final output has length same as the 
%// number of elements in X(:,2), so "throw" this to 3rd dim with permute. 
%// Then, use bsxfun to let the broadcasting being taken care off by MATLAB.
p1 = exp(-bsxfun(@times,V.',permute(X(:,2),[3 2 1]))); %//'

%// Going with same philosophy as before, get vectorized (Z.^X(j,1))
p2 = bsxfun(@power,V,permute(X(:,1),[3 2 1]));

%// Finally "merge" earlier two parts for final output
phi_out = bsxfun(@times,p1,p2);
Sign up to request clarification or add additional context in comments.

1 Comment

This worked for me quite well. Whilst the actual function unfortunately produced large values so as a result of using this I was able to successfully eliminate producing the plot I wanted (phi_out) in my project. Your answer does however have some really useful tips so I appreciate the help greatly.

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.