2

I know in Matlab you can use "nice" vector operations like A*B or A.*B

If you have

A=[2, 2]; B=[3, 1]; 

it is logic, you cannot use A*B. You can use A.*B what is A[1]*B[1], A[2]*B[2] and result is [6, 2].

In many "scripts" I am writing I often need to use something, that results in:

[6, 6; 2, 2] 

So basically i need to use forcycle (something like:):

C=zeros(2,2); for i=1:size(A,1) C(i,:)=A*B(i); end 

And i would like to ask, how (if it is possible) to rewrite this without forcycles? Is it possible? This 2 vectors of 2 elements is maybe bad for understanding what i need. So another example:

A=[1,2,3,4,5] B=[2,4] 

result:

[2,4,6,8,10; 4,8,12,16,20] 

in short SOMETHING like:
C(1:end <==== GO BY ONE ELEMENT,:)=A*B(1:end <===== GO BY ONE ELEMENT)
--> Take WHOLE vector A and multiply it by FIRST element in B and save it at FIRST row in matrix what have A columns and B rows..
--> Take WHOLE vector A and multiply it by SECOND element in B and save it at SECOND row in matrix what have A columns and B rows..
--> Take WHOLE vector A and multiply it by THIRD element in B and save it at THIRD row in matrix what have A columns and B rows..
--> ...
--> Take WHOLE vector A and multiply it by LAST element in B and save it at LAST row in matric what have A columns and B rows..

2 Answers 2

4

You can use bsxfun for this purpose

P = bsxfun(@times,A,B') 

This gives

A = 1 2 3 4 5 B = 2 4 P = 2 4 6 8 10 4 8 12 16 20 
Sign up to request clarification or add additional context in comments.

4 Comments

will try..thanks..bsxfun functions is something what i really dont understand and i thought there will be a solution..got a try that..thanks
exactly what i needed to do..works like a charm..thanks you very much..and can you please advice me a little bit more..do you have some link where bsx functions are good explained that i can maybe try to understand them?? again thank you very much. answer found..
To understand such function, you probably need to play with toy examples. Here, bsxfun uses the column vector of B (B'), takes one element at a time (first of B then second), and apply for each of them the times function (A times one value of B).
Remember to use .', not '. The vectors could be complex!
2

An alternative solution is to use matrix multiplication:

[ones(size(B))'*A].*[B'*ones(size(A))] 

5 Comments

Nice solution as well. But i think we both can agree, bsxfun is a little bit nicer solution (atleast for writing it :D)..Is there some advantages in this alternative?? For example better computing time or better memory performance?
I agree, bsxfun is probably better both in brevity and speed. The only reason this solution looks appealing for some of us is that it is intuitive and readable. It's just multiplying a [2x1] column vector with a [1x5] row vector to get the [2x5] matrix etc...
yes, for "some of us" was good said :D cause when i am trying to rewrite it exactly on my problem i still get a inner matrix dimensions error..:D..but anyway thank you very much as well..go to try to play with that..i want atleast understand that..:] thank you very much
got it..inversion by another element :]..ok i think i understand it..thank you both guys. i think i will use one of those possibilities many times in next coding..it helped me a lot
Remember to use .', not '. The vectors could be complex!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.