Is there a function that performs element by element multiplication between two variables and stores in one of the variable by default?

I want to do this operation
A = A.*B;
Is there a function
func(A, B)
that multiplies A and B and saves the result in A? If not how do we create such a function?

3 件のコメント

Why doesn't A = A.*B work?
D_coder
D_coder 2018 年 9 月 16 日
because i want to use it in a function as an argument directly inside a loop that runs about 10K times
Bruno Luong
Bruno Luong 2018 年 9 月 18 日
編集済み: Bruno Luong 2018 年 9 月 18 日
It seems MATLAB (at least with R2018b) is intelligent enough to do inplace product when calling A=A.*B as shows bellow (the pr address is dd885ac0 and does not change after calling mtimes):
>> format debug
>> a=rand(1,3)+1i*rand(1,3)
a =
Structure address = d743b820
m = 1
n = 3
pr = dd885ac0
0.9572 + 0.1419i 0.4854 + 0.4218i 0.8003 + 0.9157i
>> b=rand(1,3)+1i*rand(1,3)
b =
Structure address = d743b890
m = 1
n = 3
pr = de61eb00
0.7922 + 0.0357i 0.9595 + 0.8491i 0.6557 + 0.9340i
>> a = a .* b
a =
Structure address = d743b820
m = 1
n = 3
pr = dd885ac0
0.7532 + 0.1466i 0.1076 + 0.8168i -0.3305 + 1.3479i
>> c = a .* b
c =
Structure address = d743bd60
m = 1
n = 3
pr = 2ea50160
0.5915 + 0.1430i -0.5904 + 0.8751i -1.4757 + 0.5752i
>>

サインインしてコメントする。

回答 (2 件)

dpb
dpb 2018 年 9 月 16 日
編集済み: dpb 2018 年 9 月 16 日
Sure, there's a function for virtually everything in Matlab...all operators are are aliases for the underlying functions.
>> help times
.* Array multiply.
X.*Y denotes element-by-element multiplication. ....
...
A=times(A,B);
is functional form for A.*B but that's as far as you can get towards the Q?.
There is no syntax that supports in-place storage into one of a function's actual arguments as you're asking for. You can only store results of a function return to an assignable object on the LHS of an expression or use the results in an expression.

9 件のコメント

is there someway to do assignment as an argument of function
A = function ( A = times(A,B));
dpb
dpb 2018 年 9 月 17 日
No.
If the concern is the two instances of the variable A and you're worried about excessive memory usage that you could avoid an "extra" copy of A, don't. The ML optimizer will operate on the copy and then assign it to the variable on return.
If that's not the concern, what specific issue are you thinking you're trying to address here?
dpb
dpb 2018 年 9 月 17 日
編集済み: dpb 2018 年 9 月 17 日
You've got an active thread going with some of the best in the business; I'd recommend not starting another on the same subject.
If the combination of Bruno, Walter, and James can't beat it, you've basically reached the limits of Matlab... :)
MATLAB does not explicitly support user-level in-place operations. In some cases it does in-place operations itself. The conditions under which it does that are not clearly defined. The means to "properly" code your own in-place operations at the Mex level are not clearly defined. James is the resident effort as to what can be done in practice, and he has recently been finding some changes that were not documented which affect the ability to do in-place operations.
Furthermore, you are using complex arrays, and the storage representation for complex arrays changed a small number of releases ago, so it is important to specify exactly which version of MATLAB this needs to execute with.
If James is not able to assist you in a timely manner while he experiments without documentation during his free time, then you should probably hire Mathworks Consulting.
D_coder
D_coder 2018 年 9 月 17 日
matlab 2018b
dpb
dpb 2018 年 9 月 17 日
"James is the resident expert ..."
That too ;-)

サインインしてコメントする。

Bruno Luong
Bruno Luong 2018 年 9 月 17 日
編集済み: Bruno Luong 2018 年 9 月 17 日
If you download this FEX
there is inplaceprod.c C-file that allows to perform inplace element-wise product of vector via MEX.
You'll be careful to use such inplace operation since it is not meets MATLAB specification and can produce unexpected side effect.

カテゴリ

ヘルプ センター および File ExchangeData Type Identification についてさらに検索

質問済み:

2018 年 9 月 16 日

編集済み:

2018 年 9 月 18 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by