Turning a (4D matrix * 1D vector) operation into independent (3D matrix * 0D scalar) operations without loops - matlab

Is there any way to vectorize the following:
for i = 1:6
te = k(:,:,:,i).*(c(i));
end
I'm trying to multiply a 4D matrix, k, by a vector, c, by breaking it up into independent (3D matrix * scalar) operations. I already have two other unavoidable for loops within a while loop in this function file, and am trying my best to avoid loops.
Any insight on this will be much appreciated!
-SC

You can do this using MTIMESX - a Fast matrix multiplication tool with multidimensional support by James Tursa, found in Matlab's file exchange.
It is as simple as:
C = mtimesx(A,B)
performs the calculation C = A * B

Unless I'm missing something, this is a case for bsxfun:
te=bsxfun(#times, k, permute(c,[3 4 1 2])); % c is a row vector
Or
te=bsxfun(#times, k, permute(c,[3 4 2 1])); % c is a column vector
This is assuming that the 4th dimension of k has the same size as c. If not, then you can use submatrix indexing:
te=bsxfun(#times, k(:,:,:,1:length(c)), permute(c,[3 4 2 1])); % c is a column vector

Related

How to extend a column vector to a matrix in matlab

For a vector a = [1 2 3]', I want to extend it to matrix [1:1+n;2:2+n;3:3+n]. Just like extend a number x to a sequential vector by x:x+a.
You can use bsxfun to add 0:n to each element in a
b = bsxfun(#plus, a, 0:n)
Or if you're using MATLAB R2016b, you can replace the bsxfun approach with the following thanks to automatic broadcasting of dimensions.
b = a + 0:n

Matrix Multiplication Issue - Matlab

In an attempt to create my own covariance function in MatLab I need to perform matrix multiplication on a row to create a matrix.
Given a matrix D where
D = [-2.2769 0.8746
0.6690 -0.4720
-1.0030 -0.9188
2.6111 0.5162]
Now for each row I need manufacture a matrix. For example the first row R = [-2.2770, 0.8746] I would want the matrix M to be returned where M = [5.1847, -1.9915; -1.9915, 0.7649].
Below is what I have written so far. I am asking for some advice to explain how to use matrix multiplication on a rows to produce matrices?
% Find matrices using matrix multiplication
for i=1:size(D, 1)
P1 = (D(i,:))
P2 = transpose(P1)
M = P1*P2
end
You are trying to compute the outer product of each row with itself stored as individual slices in a 3D matrix.
Your code almost works. What you're doing instead is computing the inner product or the dot product of each row with itself. As such it'll give you a single number instead of a matrix. You need to change the transpose operation so that it's done on P1 not P2 and P2 will now simply be P1. Also you are overwriting the matrix M at each iteration. I'm assuming you'd like to store these as individual slices in a 3D matrix. To do this, allocate a 3D matrix where each 2D slice has an equal number of rows and columns which is the number of columns in D while the total number of slices is equal to the total number of rows in D. Then just index into each slice and place the result accordingly:
M = zeros(size(D,2), size(D,2), size(D,1));
% Find matrices using matrix multiplication
for ii=1:size(D, 1)
P = D(ii,:);
M(:,:,ii) = P.'*P;
end
We get:
>> M
M(:,:,1) =
5.18427361 -1.99137674
-1.99137674 0.76492516
M(:,:,2) =
0.447561 -0.315768
-0.315768 0.222784
M(:,:,3) =
1.006009 0.9215564
0.9215564 0.84419344
M(:,:,4) =
6.81784321 1.34784982
1.34784982 0.26646244
Depending on your taste, I would recommend using bsxfun to help you perform the same operation but perhaps doing it faster:
M = bsxfun(#times, permute(D, [2 3 1]), permute(D, [3 2 1]));
In fact, this solution is related to a similar question I asked in the past: Efficiently compute a 3D matrix of outer products - MATLAB. The only difference is that the question wanted to find the outer product of columns instead of the rows.
The way the code works is that we shift the dimensions with permute of D so that we get two matrices of the sizes 2 x 1 x 4 and 1 x 2 x 4. By performing bsxfun and specifying the times function, this allows you to efficiently compute the matrix of outer products per slice simultaneously.

Multiply two matrices in Matlab to obtain 3-dimensional matrix

I have two sparse matrices in Matlab, A and B,
and I want to compute a three-dimensional matrix C such that
C(i,j,k) = A(i,j) * B(j,k)
can I do this without a loop?
(Side question: Is there a name for this operation?)
Edit:
Seems my question has already been asked (just for full matrices):
Create a 3-dim matrix from two 2-dim matrices
For full matrices:
You can do it using bsxfun and shiftdim:
C = bsxfun(#times, A, shiftdim(B,-1))
Explanation: Let A be of size M x N and B of size N x P. Applying shiftdim(B,-1) gives a 1 x N x P array. bsxfun implicitly replicates A along the third dimension and shiftdim(B,-1) along the first to compute the desired element-wise product.
Another possibility, usually less efficient than bsxfun, is to repeat the arrays explicity along the desired dimensions, using repmat:
C = repmat(A, [1 1 size(B,2)]) .* repmat(shiftdim(B,-1), [size(A,1) 1 1])
For sparse matrices:
The result cannot be sparse, as sparse ND-arrays are not supported.. But you can do the computations with sparse A and B using linear indexing:
ind1 = repmat(1:numel(A),1,size(B,2));
ind2 = repmat(1:numel(B),size(A,1),1);
ind2 = ind2(:).';
C = NaN([size(A,1),size(A,2),size(B,2)]); %// preallocate with appropriate shape
C(:) = full(A(ind1).*B(ind2)); %// need to use full if C is to be 3D
Answer to your side question: the name for this operation is a hash join.

Linear equations with multidimensional matrices [Modified]

There is a system of 3 linear equations composing of matrices which are represented by RGB image. Say
A = A1*x1 + A2*x2 + A3*x3 ......(Eq 1)
B= A1*x4 + A2*x5 + A3*x6 ........(Eq 2)
C= A1*x7 + A2*x8 + A3*x9 ........(Eq 3)
each are of equal dimension say 3D. I performed the following
A11=rgb2gray(A1);
x11=rgb2gray(x1);
A11 =double(A1) ; x11 = double(x11); b = A1*x1;
opts.UT = true; opts.TRANSA = false;
y1 = linsolve(x1,b,opts);
imshow(y1);
% The objective is to obtain A1,A2,A3
On doing this, following issues have surfaced:
1. Error
The output y1 is not the same as A1, which should have been. Why is it so? Please help
The R,G and B spaces are orthogonal. So you can solve each of those sets independently. The problem here is that mtimes, which is your matrix multiplication operator, doesn't accept 3D inputs.
To solve this, you can loop through each of R, G and B and use linsolve for each of the resulting 2D matrices. Normally, I wouldn't recommend loops for anything in MATLAB, but here, there won't be any discernable overhead as there are only 3 iterations in the loop.
Your answer will not be any different from what it would be if you were to solve them all in one go (if that were possible), because the three spaces are independent.
EDIT
The way you've written your equations, the xi's form the coefficient matrix and Ai's are the unknowns. The system of equations can be written compactly as XY=Z, where X is a 3D matrix composed of the coefficients, xi for each color space,RGB; Y is a 2D matrix, with a vector [A1, A2, A3]' in each color space, and Z is also a 2D matrix with vectors [A, B, C]' in each color space.
Assuming that the colorspace is the last dimension, you can try
[xPixels,yPixels,colorSpace]=size(X);
Y=zeros(yPixels,colorspace);
opts.UT=true; opts.TRANSA=false;
for i=1:colorspace
Y(:,i)=linsolve(X(:,:,i),Z(:,i),opts);
end
You'll have to setup the matrices X, Y and Z according to your problem. It is helpful to keep the looped dimension (in this case, colorspace) as the outermost dimension, as otherwise, you'll have to use squeeze to remove the singleton dimensions.

Vector to Matrix syntax in MATLAB

Is there a way to combine 2 vectors in MATLAB such that:
mat = zeros(length(C),length(S));
for j=1:length(C)
mat(j,:)=C(j)*S;
end
Using normal MATLAB syntax similar to:
mat = C * S(1:length(S))
This gives a "Inner matrix dimensions must agree error" because it's trying to do normal matrix operations. This is not a standard Linear Algebra operation so I'm not sure how to correctly express it in MATLAB, but it seems like it should be possible without requiring a loop, which is excessively slow in MATLAB.
From your description, it sounds like a simple matrix operation. You just have to make sure you have the right dimensions for C and S. C should be a column vector (length(C)-by-1) and S should be a row vector (1-by-length(S)). Assuming they are the right dimensions, just do the following:
mat = C*S;
If you're not sure of their dimensions, this should work:
mat = (C(:))*(S(:)');
EDIT: Actually, I went a little crazy with the parentheses. Some of them are unnecessary, since there are no order-of-operation concerns. Here's a cleaner version:
mat = C(:)*S(:)';
EXPLANATION:
The matrix multiplication operator in MATLAB will produce either an inner product (resulting in a scalar value) or an outer product (resulting in a matrix) depending on the dimensions of the vectors it is applied to.
The last equation above produces an outer product because of the use of the colon operator to reshape the dimensions of the vector arguments. The syntax C(:) reshapes the contents of C into a single column vector. The syntax S(:)' reshapes the contents of S into a column vector, then transposes it into a row vector. When multiplied, this results in a matrix of size (length(C)-by-length(S)).
Note: This use of the colon operator is applicable to vectors and matrices of any dimension, allowing you to reshape their contents into a single column vector (which makes some operations easier, as shown by this other SO question).
Try executing this in MATLAB:
mat = C*S'
As In:
C = [1; 2; 3];
S = [2; 2; 9; 1];
mat = zeros(length(C),length(S));
for j=1:length(C)
mat(j,:)=C(j)*S;
end
% Equivalent code:
mat2 = C*S';
myDiff = mat - mat2
Do you mean the following?
mat = zeros(length(C),length(S));
for j=1:length(C)
mat(j,:)=C(j)*S;
end
If so, it's simply matrix multiplication:
C' * S % if C and S are row vectors
C * S' % if C and S are column vectors
If you don't know whether C and S are row vectors or column vectors, you can use a trick to turn them into column vectors, then transpose S before multiplying them:
C(:) * S(:)'
I'm not entirely clear on what you're doing - it looks like your resulting matrix will consist of length(C) rows, where the ith row is the vector S scaled by the ith entry of C (since subscripting a vector gives a scalar). In this case, you can do something like
mat = repmat(C,[1 length(S)]) .* repmat(S, [length(C) 1])
where you tile C across columns, and S down rows.
Try this:
C = 1:3
S = 1:5
mat1 = C'*S
mat2 = bsxfun(#times, C',S)
(esp. good when the function you need isn't simpler MATLAB notation)
--Loren
Try using meshgrid:
[Cm, Sm] = meshgrid(C, S);
mat = Cm .* Sm;
edit: nevermind, matrix multiplication will do too. You just need one column vector C and one row vector S. Then do C * S.