0
votes

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?

3
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? - Ben Voigt

3 Answers

1
votes

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
0
votes

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
0
votes

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)));