'Out of memory' error for element-wise vector product '.*'
3 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
theophrasth
il 25 Apr 2019
Commentato: Steven Lord
il 25 Apr 2019
I have two quite large double vectors (about 10e6 elements) that can have a different orientation (column or row) and i want to multiplicate them element-wise.
Therefore i use the '.*'-operator to multiply the two vectors. But this works only satisfactorily for vectors that have the same orientation (e. g. two column vectors).
Otherwise i get an 'Out of memory' exception.
This is not what i excpected because i thought the '.*'-operators internal behaviour wouldn't depend on the orientation of the input vectors. But apparently it does.
Here is an example that should clearify my point:
% Maybe has to be adjusted depending on the amount of available memory on your system (tested with 16 GB of RAM)
x = 0 : 1/10e6 : 1;
y = x .* x; % works
y = x' .* x'; % works
y = x .* x'; % out-of-memory exception
y = x' .* x; % out-of-memory exception
Now i am asking myself if i didn't understand how to correctly make use of this operator?
Or is this kind of a "bug" (maybe "inefficiency" is more appropriate) in the implementation of the '.*'-operator?
1 Commento
Torsten
il 25 Apr 2019
Choose
x = 0 : 0.1 : 1
and display the sizes of the different y variables you defined above:
y = x.*x;
size(y)
...
Risposta accettata
Guillaume
il 25 Apr 2019
Prior to R2016b, matlab worked the way you expected. The shape of the vectors did not matter. In R2016b, mathworks changed the behaviour to implicitly expand vectors (or matrices) with different shape when they have compatible sizes. There was much gnashing of teeth from many. To me, it makes sense, it just continues to higher dimensions the implicit scalar expansion that already existed.
Suffice to say, what you see is not a bug but expected behaviour since R2016b. If your vectors can be any shape, the simplest way to ensure that you don't trigger implicit expansion is to reshape them as column vectors:
X(:) .* y(:)
On the other hand, the fact that your vectors can come either as columns or rows should give some concern.
2 Commenti
Steven Lord
il 25 Apr 2019
If you want to check ahead of time that the vectors have the same orientation, you can use isrow, iscolumn, and isvector to determine if you need to "columnize" the vectors.
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Matrices and Arrays in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!