I have a m x n x p 3D matrix available where, m x n are 2D images (row * columns), and p is the number of images.
I need to make this matrix 4D such that the new dimensions are m x n x 1 x p. The third dimension is constant for each of the images.
How can I do this in MATLAB?
A call to permute should do the trick. Supposing that your image is stored in A, just do:
B = permute(A, [1 2 4 3]);
This transforms your matrix, which is m x n x p, to a matrix with a singleton third dimension while changing the third dimension from the original matrix so that it now becomes the fourth dimension.
Related
I have a matrix X with size N x N, and a vector Y with size NM x 1. I want to multiply the matrix X with every N elements in Y and then reshape the resultant matrix Z row-wise. In other words, I first reshape the vector Y into a matrix Y_2 with size N x M . Then, get the matrix Z = X * Y_2, and finally reshape the matrix Z row-wise.
That process, I want to do it in matlab using matrices multiplications, as follows:
clear all; clc; clear;
N = 4; M = 8;
X = randn(N,N);
Y = randn(N*M,1);
Z = kron(eye(M,M),X) * Y;
The problem, is that I don't get $Z$ similar to that process explained above. I mean the result of the way I used in Matlab code is not similar to the results of the process explained before. how can I do it, where is it error in my other method?
I have a matrix of size N -by- N and I want to create a matrix (10 N) -by- (10 N), where each N -by- N block is a copy of the original matrix. I would like to do it without for loops.
I have tried with the function kron, but it only "enlarges" the original matrix.
How can I make this matrix?
You're looking for repmat()
A = rand(10); % Original 10 x 10 matrix
B = repmat(A, 10); % Copied 10 times in each direction
I had to convert an n x n matrix to n^2 x 1 column vector for ease of some operations. Now, that the operations are done, how do I return to the n x n form from the n^2 x 1 vector.
It is supposed to be opposite of this: concatenation
Thanks!
You can use the reshape() function:
//M is your n^2 x 1 column vector, A is your nxn matrix that you want to recover
A = reshape(M, [n n])
If your n x n matrix is 3x3, then:
A = reshape(M, [3 3])
For more info: http://www.mathworks.com/help/matlab/ref/reshape.html
Vector x is 2000-by-1
I would like to take the first 20 elements of vector x and copy them to vector y, then copy the next 20 elements of vector x to vector z, then copy the next 20 elements to vector y, and so on.
I understand I could do this with a loop, but am hoping to find a more efficient method.
This can be achieved by reshaping the vector to a matrix, selecting odd/even columns and finally flattening the matrix:
m = reshape(a, 20, []);
x = m(:,1:2:end); x = x(:);
z = m(:,2:2:end); z = z(:);
I have a n x m x d matrix A (i.e. A is like d n x m matrices). I would like to convert this into one n x m matrix B where each element B(i,j) is function of A(i,j,1), ..., A(i,j,d), more specifically the L2 norm of these values:
B(i,j) = sqrt[A(i,j,1)^2 + ... + A(i,j,d)^2]
Meaning I would like to condens or "flatten" the information in matrix A. How can I achieve this without resorting to a nested for loop?
Do elementwise squaring and sum along the third dimension to produce a N x M matrix and then apply square-root for a vectorized implementation, like so -
B = sqrt(sum(A.^2,3))