How to get thin QR decomposition in Julia? - matrix

When I perform QR decomposition on a 3x2 matrix A in Julia, it gives a 3x3 matrix Q. Is there any way I can get a "thin" version of this QR, where it returns a Q that is 3x2 (same dimensions as matrix A)? My goal is just to get an orthonormal basis for the column space of A, so I don't need a 3x3 matrix Q.

This can be achieved with Matrix(qr(A)). qr doesn't return matrices, but rather returns an object that can multiply by other matrices or easily extract the thin or full Q matrix.

Related

How to generate an orthogonal symmetric matrix?

I want to create an orthogonal symmetric matrix of size 5 and size 6. Is there a way for me to generate this type of matrix? An orthogonal symmetric matrix is when a matrix A is equal to its transpose and also its inverse.
I've tried searching for ways to do this but all I get is how to generate an orthogonal matrix or how to generate a symmetric matrix. Couldn't find a way to generate an orthogonal symmetric matrix.

How do I calculate the mean shared error between two 3D tensors in matrix form?

I know that the mse between two 2d matrices, A and B, of shapes pxq, can be calculated in matrix terms as follows:
1/n tr(AtB)
The nice thing about this equation is that the matrices At and B are conformal to matrix multiplication and also yields a square matrix which has a defined trace.
But if we have two 3d tensors A and B of shapes pxqxr, then I don't understand how to get the outer product between them to get a square matrix so that the mse camne written in terms of trace.

Dot product of a square rotation matrix to each element of another coordinate matrix

I have a 3D matrix containing Cartesian coordinate points. In the example shown below a scaled down version of such a matrix is given (a) with dimensions 10x10x10x3. Then I have a rotation matrix R of size 3x3 and I want to make a dot product of this to each 3x1 position vector given in the matrix a, for example a[0,0,0] is the position vector for top left most coordinate point.
A simple way to do this is by using a for loop -
import numpy as np
a = np.ones([10, 10, 10, 3])
R=np.ones([3,3])
a=np.reshape(a,(1000,3))
b=np.array([np.dot(R,xyz) for xyz in a])
b=np.reshape(b, (10,10,10,3))
But this is way too slow when the matrix a becomes large. Is there a way to do a matrix multiplication type thing to speed up the computation?
I figured out a solution to my problem which speeds up the computation to more than 10 folds. But it is not a fancy matrix multiplication solution, so something that can make it even faster is always appreciated.
My current solution is the following:
X,Y,Z=a[:,:,:,0],a[:,:,:,1],a[:,:,:,2]
R00X=R[0][0]*X
R10X=R[1][0]*X
R20X=R[2][0]*X
R01Y=R[0][1]*Y
R11Y=R[1][1]*Y
R21Y=R[2][1]*Y
R02Z=R[0][2]*Z
R12Z=R[1][2]*Z
R22Z=R[2][2]*Z
#Rotated Field
Xr=R00X+R01Y+R02Z
Yr=R10X+R11Y+R12Z
Zr=R20X+R21Y+R22Z
b=np.array([Xr,Yr,Zr]).T

Matlab - Image Formation - Matrix

I am doing a very interesting Computer Vision project which talks about how to "create manually" images with Matlab.
The teacher gave me three matrices: the illuminant matrix (called E), the camera sensitivity matrix (called R) and finally, the surface reflectance matrix (called S).
The matrix dimensions are as follows:
S: 31x512x512 (reflectance samples x x-dimension x y-dimension)
R: 31x3
E: 31x1
The teacher gave me also the following relationship:
P=transpose(C)*R=transpose(S)*diagonal(E)*R
Where C is the color matrix.
Where P is the sensor response matrix.
The goal is to display the image formed by all the previous matrices. Therefore, we have to compute the P matrix.
The class of all the matrices is double.
This is what I have done:
Diag_D=diag(D);% Diagonal matrix of D
S_reshaped= reshape(S,31,[512*512]);% Reshape the surface reflectance matrix
S_permute=permute(S_reshaped,[2 1]);% The output matrix is a 262144x31 matrix
Color_Signal_D65_buffer=S_permute*Diag_DD;
Color_Signal_D65=reshape(Color_Signal_D65_buffer,[512 512 31]);% This is the final color matrix
Image_D65_buffer= (reshape(Color_Signal_D65,[512*512],31))*R;% Apply the given formula
Image_D65= reshape(Image_D65_buffer,[512 512 3]);% image formation
Image_D65_norm=sqrt(sum(Image_D65.^2,3));% Compute the Image_D65 norm
Image_D65_Normalized=bsxfun(#rdivide, Image_D65, Image_D65_norm);% Divide each element of the matrix by the norm in order to normalize the matrix
figure
imshow(Image_D65_Normalized)% Display the image
However,it did not work at all. The output is an image but the colors are completely wrong (there is too much blue on the image).
I think it could be a matrix reshaping problem but I have tried all the possible combinations but nothing to do.
Thank you so much for your help
I've finaly found the error. It was a problem in the normalization process. I was using the wrong formula.

Calculate covariance of two images in matlab

I am doing a project on image quality assessment. I converted the image to grayscale & divided the entire image into 8x8 matrices using mat2cell function. I did this for two images, and now I want to calculate covariance between these two images (i.e the covariance between the matrix of image 1 and covariance between the same matrix of image 2). Note that both are the same images: one a pure image without distortions and one with distortions.
First convert your image to matrix:
I = double(imread('photo.jpg'));
then calculate covariance:
x=cov(I);
For single matrix input, C has size [size(A,2) size(A,2)] based on the number of random variables (columns) represented by A. The variances of the columns are along the diagonal. If A is a row or column vector, C is the scalar-valued variance.
For two-vector or two-matrix input, C is the 2-by-2 covariance matrix between the two random variables. The variances are along the diagonal of C.

Resources