vectorization by rows instead of elements

5 Ansichten (letzte 30 Tage)
Eugene
Eugene am 21 Aug. 2016
Bearbeitet: dpb am 23 Aug. 2016
Is there a way to vectorize a for loop so that a function operates on a row instead of individual elements? I'm seeing significant computation speed improvements when I vectorize, so I lean towards that.
I know that if I have a matrix A, that I can do A.^2 to square each element of the matrix. But what if, for example, each row represents coordinates and I want to find the magnitude of the row vector which is the sum of the squares:
M(1) = sqrt(A(1,1)^2 + A(1,2)^2 + ... A(1,n)^2)
So, if I have a function Mag(a1,a2,...an), I'd like to do something like:
M = Mag(A)
If A is m x n, then M is m x 1, with each row representing the magnitude of each set of row coordinates in A. In my case, n is known ahead of time and I don't (necessarily) need a variable number of arguments.
While I know that I can do the Mag function by squaring the elements and summing etc., my actual function is much more complicated and cannot be reduced. This method would also save me from having to enumerate every column value when it operates on rows.
While I could do this in a loop, I'd like to know, is there is a way to vectorize as I explained?

Antworten (1)

dpb
dpb am 22 Aug. 2016
Well, without your other function, no can say, but
M(1) = sqrt(A(1,1)^2 + A(1,2)^2 + ... A(1,n)^2)
is simply
M=sqrt(sum(y.^2,2));
or
M=sqrt(dot(y,y,2));
  2 Kommentare
Eugene
Eugene am 22 Aug. 2016
As I indicated, the function I gave was just a simple example of what I'm trying to do. Mag is easily vectorized. This solution doesn't work for my function as it cannot work on individual elements. That's the point, that I can't separate easily like you did, it needs all the inputs at the same time.
Here's the formula: Black Scholes Formula
However, I'm not only trying to vectorize this specific function, but I want to know if there's a way to have a function take an entire row as input.
dpb
dpb am 22 Aug. 2016
Bearbeitet: dpb am 23 Aug. 2016
I don't see anything peculiar about the formula preventing it from being vectorized.
We need more specific illustration of just what you mean or see as a problem...where did you get stuck, specifically in implementing what it is you're trying to implement?
As for "have a function take an entire row as input", sure, pass it.
Is there a builtin function rowfun that does this automagically, no, but what do you see as "needs all the inputs at the same time" as being somehow an issue; if you have the inputs you just compute the result; if you don't have 'em you're stuck anyhoo...
ADDENDUM
BTW, the thing to take from the first function solution is not that it isn't your specific function but the use of the features in Matlab beyond the default to vectorize by row via the use of the optional direction parameter in the function. Not every function in Matlab has this feature, but many do and the key in implementation of a specific computation is to recognize first there are such features to be used and then thinking about how to structure the specific problem to take advantage of the language syntax and builtin functions most advantageously. It's not clear to me the general principles involved here are being recognized in lieu of the specific function "getting in the way" of seeing the bigger picture.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by