0

I'd like to multiply a random number vector PT(n)=rand(1,n) by a matrix M(mxn) but want to have a different random vector for each column multiplication. Is it possible in Matlab?

E.g. PT=rand(1,4);

`PT*(1 0 0 0;... 0 0 0 1;... 0 1 0 0;... 0 0 0 1); 

but where PT changes for each column multiplication. The only way I can think of is make PT=rand(4,4)and then take diag(PT*M) but it's very expensive if my matrix M is large.
Any thoughts?

Cheers

Suplemental using @Nasser arrayfun code takes 3 times longer than a for loop. I see it's normal but why the big difference?

1
  • You can't multiply a 1x n vector by a mx n matrix. And wouldn't what you describe just result in a random vector anyway? Commented Jan 14, 2012 at 5:47

3 Answers 3

1

I am not sure if I understood exactly what you are asking.

But if you mean you have a matrix of vectors, and you want to multiply another matrix by each one of these vectors then one way is to use arrayfun.

For example: Here we multiply a 5 by 4 matrix with 3 vectors, each is 4 by 1.

The result is 3 vectors, each is 5 by 1

pt = rand(4,3); M = rand(5,4); r = arrayfun(@(i) M*pt(:,i),1:size(pt,2),'UniformOutput',false) 

gives

r = [5x1 double] [5x1 double] [5x1 double] cell2mat(r) ans = 0.1463 0.4386 0.4638 0.4104 0.8105 0.6455 0.9503 1.0145 1.0369 1.3011 1.4583 1.5233 0.4688 0.7405 0.7492 
Sign up to request clarification or add additional context in comments.

5 Comments

That's interesting but still makes n times more data than I need. I just need to the top row so don't want to make a huge matrix. Can I specify which rows to multiply in that arrayfun eg. PT(:,i)*M(i,:)
I think a small modification of this will be just right: r = arrayfun(@(i) M(i,:)*pt(:,i),1:size(pt,2),'UniformOutput',false)
OK, if that works for you. But the above is a vector by vector multiplication now (you are multiplying a row vector by a column vector). You said in your question you wanted to multiply a matrix by a vector. Either way, glad to be of help.
Thank you for your careful explanation. I have implemented this and eventually found a for loop to be time saving... Could you comment on a case in which it would be better practice to use arrayfun rather than a loop? A google search upholds the claim of speed, but doesn't give a good reason for one over the other. What do you think? Thanks
loops are fast now in Matlab (since 6.5). But this is just a styling issue. I like functional programming, and so arrayfun seems natural to me. You are applying a function on an array. So, any time you have a function, and list of things you want to apply this function on, you can either use vectorization (if possible), if not, then either use a loop or arrayfun (unless something like bsxfun ofcourse will do, then use that). in arrayfun, the function you are applying can be a handle to the actual function, and so it is a flexible operation. use what feels better for you (all things equal).
0

If I'm following you, how about

M = rand(4,4); % your matrix PT = rand(4,4); % your random row vectors rslt = sum(PT'.*M,2); % your desired result 

1 Comment

Nice! Yes that solves the problems without for loops. In fact all I was looking for, as it turns out, was PT'.*M. Strange how the mind blocks out the simplest solutions. It's actually made simplifications in other parts of the code too! Thanks again!
0

I ma not sure, but according to your example, it looks like you want to do a random permutation of the columns of PT. If that's the case, you can do:

PT=PT(:,randperm(size(PT,2))); 

1 Comment

This permutes the columns in the array but can I make the values of the vector PT(1,4) vary randomly after every column multiplication with M(:,i)?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.