Matlab vectorization of cartesian product - matlab

Let's say I have two arrays X and Y and I need a matrix M(i, j) = some_func(X(i), Y(j)). How can I get that without using loops?

The best answer is to use bsxfun, IF it is an option. According to the help for bsxfun, it will work on any general dyadic function, as long as:
FUNC can also be a handle to any binary element-wise function not listed
above. A binary element-wise function in the form of C = FUNC(A,B)
accepts arrays A and B of arbitrary but equal size and returns output
of the same size. Each element in the output array C is the result
of an operation on the corresponding elements of A and B only. FUNC must
also support scalar expansion, such that if A or B is a scalar, C is the
result of applying the scalar to every element in the other input array.
If your function accepts only scalar input, then loops are the simple alternative.

It's difficult to answer your vague question, and it ultimately depends on your function. What you can do is use meshgrid and then perform your operation, typically with use of the dot operator
e.g.
x = 1:5;
y = 1:3;
[X,Y] = meshgrid (x,y)
M = X.^Y;

Related

MATLAB: element-wise multiplication of two matrices over one index?

I'm trying to figure out if there's a native way of obtaining a certain kind of element-wise product of two matrices in Matlab.
The product that I'm looking for takes two matrices, A and B say, and returns there product C, whose elements are given by
C(i,j,k) = A(i,j)*B(j,k)
Naturally, the number of columns of A is assumed be the same as the number of rows of B.
Right now, I'm using the following for-loop (assuming size(A,2)==size(B,1) is true). First, I initialize C:
C = zeros(size(A,1), size(A,2), size(B,2));
And then I perform element-wise multiplication via:
for i=1:size(A,2)
C(:,i,:) = A(:,i)*B(i,:);
end
So, my question is: Is there a native way to this sort of thing in Matlab?
You need to "shift" the first two dimensions of B into second and third dimensions respectively with permute and then use bsxfun with #times option to operate on A and the shifted dimension version of B -
C = bsxfun(#times,A,permute(B,[3 1 2]))

Find minimum of following function in Matlab

I am trying to minimize sum((A-r*B).^2) in Matlab where A and B are matrices and r is the scalar I am manipulating. I tried the following code:
f = #(r) sum((A-r*B).^2);
Answer = fminbnd(f,lowrange,highrange);
But I get an error.
If A and B are matrices, then sum((A - r*B).^2) will not give you a single value. This will give you an array of values. If the input to sum is a matrix, the output of sum will give you an array where each element is the sum along the rows for each column in your matrix.
The function you are specifying to fminbnd must evaluate to a single value. I'm assuming you want to determine the sum of squared differences in your function, and so you need to wrap your sum with another sum. As such, try this instead:
f = #(r) sum(sum((A-r*B).^2));
Answer = fminbnd(f,lowrange,highrange);
The function f will now find the difference between matrices A and B which is weighted by r, square these differences and then add up all of these differences together.
Try that and see if it works.
If you don't need to impose limits on the possible values of the optimized scalar r, then you should be able to solve this equation directly without searching for the minimum. If A and B are vectors, use:
ropt=(B'*B)^(-1)*B'*A;
If A and B are arrays and you want to minimize the sum of squared residuals of all elements of the arrays then I believe the following will work (same formula, but convert A and B to vectors).
ropt=(B(:)'*B(:))^(-1)*B(:)'*A(:);
Examples:
b=[1;2;3]
a=2*b;
ropt=(b'*b)^(-1)*b'*a
returns ropt=2.0000, as desired
Similarly, for a matrix:
b=magic(3);
a=2*b;
ropt=(b(:)'*b(:))^(-1)*b(:)'*a(:)
also returns ropt=2.0000. This should work fine even if there isn't a perfect solution, as there is in the examples above.

MATLAB: why transpose single dimension array

I have the following Matlab code snippet that I'm having to translate to VBScript. However, I'm not understanding why the last line is even necessary.
clear i
for i = 1:numb_days
doy(i) = floor(dt_daily(i) - datenum(2012,12,31,0,0,0));
end
doy = doy';
Looking over the rest of the code, this happens in a lot of other places where there are single dimension arrays (?) being transposed in place. I'm a newbie when it comes to both these languages, as well as posting a question on Stack, as I'm a sleuth when it comes to finding answers, just not in this case. Thanks in advance.
All "arrays" in MATLAB have at least two dimensions, and can be treated as having any number of dimensions you wish. The transpose operator here is converting between a row (size [1 N] array) and a column (size [N 1] array). This can be significant when it comes to either concatenating the arrays, or performing other operations.
Conceptually, the dimension vector of a MATLAB array has as many trailing 1s as is required to perform an operation. This means that you can index any MATLAB array with any number of subscripts, providing you don't exceed the bounds, like so:
x = magic(4); % 4-by-4 square matrix
x(2,3,1,1,1) % pick an element
One final note: the ' operator is the complex-conjugate transpose CTRANSPOSE. The .' operator is the ordinary TRANSPOSE operator.

Matlab outer product like function? How?

I have the following computation I'd like to vectorize in matlab.
I have a N x 3 array, call it a.
I have a 4 x 1 cell array of function handles, call them b.
I would like to create an Nx4 matrix c, such that c(i,j) = b{j}(a(i,:).
b is actually an array, but I don't know how to write down my representation for c in a format that matlab would understand that uses a matrix.
If your function handles work on arrays (i.e. b{j}(a) returns a Nx1 array in your example), you can use CELLFUN and CELL2MAT to generate your output array:
c = cell2mat( cellfun( #(bFun)bFun(a),b,'UniformOutput',false) );
If your function handles only work on individual rows (i.e. b{j} needs to be applied to every row of a separately, you can throw ARRAYFUN into the mix (readability suffers a bit though; basically, you're applying each element of b via cellfun to each row of a via arrayfun):
c = cell2mat(...
cellfun( #(bFun)arrayfun(...
#(row)bFun(a(row,:)),1:size(a,1)),...
b,'UniformOutput',false) ...
);
pdist2 almost solves the problem above. Probably someone more clever than me can figure out how to shoe horn the two together.

MATLAB: how to apply function componentwise

Say I have a function calculateStuff(x) that takes in a scalar as parameter and returns a scalar.
Say I have a vector X and I want to apply calculateStuff on every component in X, and get a vector of the results in return and store it in a new vector Y.
Clearly Y=calculateStuff(X) is illegal, is there a way I can do this besides looping?
You have three options:
modify calculateStuff so that it can take arrays and return arrays
write a loop
use arrayfun to hide the loop: Y = arrayfun(#calculateStuff,X)
Most Matlab operations will let you input a matrix and return a matrix. You should be able to re-write calculateStuff() to take a matrix and return a matrix. That is generally MUCH faster than using a for loop. Loops in Matlab are very expensive time-wise.
The sorts of things you need to look at are "dot" versions of normal operations. For example instead of
y = z * x;
do
y = z .* x;
The first will do a matrix multiplication, which is probably not what you want when vectorizing code. The second does an element-by-element multiplication of z and x.
See here and search for "The dot operations".