Reshape 3d matrix in z direction - matlab

we have a = [1 2; 3 4]; and b = [5 6;7 8]; and z = cat(3,a,b). I want to vectorize the 3d matrix so the reresult would be c = [1 5; 2 6; 3 7; 4 8 ]? I know it is related to reshape but I can not find the way:)
Thanks.

If you need to go via z:
c = reshape(permute(z,[2 1 3]),[],2)
Otherwise,
c = [reshape(a',[],1),reshape(b',[],1)];

reshape(permute(z,[2 1 3]), 4, 2)

Related

Resizing a 3D matrix into a 2D matrix in MATLAB

I have the following three-dimensional matrix X of size 2*2*3:
X = zeros(2, 2, 3);
X(:,:,1) = [1 2; 3 4];
X(:,:,2) = [5 6; 7 8];
X(:,:,3) = [9 10; 11 12];
I want to automatically reshape X to a two-dimensional matrix of size 6*2 such that I should obtain:
X_reshaped = [1 2; 3 4; 5 6; 7 8; 9 10; 11 12].
In MATLAB, I am trying to write X_reshaped = reshape(X, [size(X,1)*size(X,3), size(X,2)]), but this doesn't give me the desired X_reshaped as above.
Any help will be very appreciated!
Using the permute() function will allow the array to be reshaped so that the elements are stored in pairs along the first dimension. This allows the reshape() function to access those pairs accordingly by default.
X = zeros(2, 2, 3);
X(:,:,1) = [1 2; 3 4];
X(:,:,2) = [5 6; 7 8];
X(:,:,3) = [9 10; 11 12];
reshape(permute(X,[1 3 2]),[6 2])

How to get a matrix that has the common elements of two matrices?

Suppose I have two matrices, A and B.
A = [2 8 4; 7 3 9];
B = [2 1 6; 1 3 9];
I'd like to get a matrix C that is as follows:
C = [2 0 0; 0 3 9];
C is a matrix that retains the common elements of A and B but changes the rest of the elements to zero. I could use a for loop and iterate over every element in both A and B but is there a more efficient method to obtain the results?
Assuming both matrices have same dimensions.
A = [2 8 4; 7 3 9];
B = [2 1 6; 1 3 9];
C = zeros(size(A));
C(A == B) = A(A == B);
C =
2 0 0
0 3 9
Another possibility is to use
C = A.*(B==A);

inverse reshape in matlab

I have used the function reshape() to help change sizes from a 3D matrix to a 2D one. Eg :
a = [1 2; 3 4];
b = [5 6; 7 8];
c = cat(3,a,b);
Here c is the matrix with 3 dimensions. I used reshape to change it thus:
[n,m,d] = size(c);
d = reshape(c, [n*m , d]);
The size of d is 4x2.
After this operation, how do I get back c from d? What I would like to know is the inverse of the reshape function.
Thank you.
Slightly modifying your notations (you have 2 d):
a = [1 2; 3 4];
b = [5 6; 7 8];
c = cat(3,a,b);
[n,m,d] = size(c);
dd = reshape(c, [n*m , d]);
cc = reshape(dd, [n, m , d]);
and you can check that cc is equal to c.
I think the problem is you overwrote the depth of the array d to be the reshaped array. Try:
[n,m,p] = size(c);
d = reshape(c, [n*m , p]);
reshape(d,[n m p])
Result:
ans(:,:,1) =
1 2
3 4
ans(:,:,2) =
5 6
7 8

Find the minimum positive difference between elements in vector

A = [1 3 5 8]
B = [1 2 3 4 5 6 7 8]
I would like to create a vector C which returns the rownumber of the element in vector A with the smallest non-negative difference to each element in vector B.
So, given the example above, it should return:
C = [1 2 2 3 3 4 4 4]
I'm sure there are many ways to do this. Here's one:
A = [1 3 5 8]
B = [1 2 3 4 5 6 7 8]
%create matrices of the values to subtract
[a,b] = meshgrid(A,B);
%subtract
aLessB = a-b;
%make sure we don't use the negative values
aLessB(aLessB < 0) = Inf;
%sort the subtracted matrix
[dum, idx] = sort(aLessB, 2, 'ascend');
idx(:,1) is the solution you are looking for.
An alternative solution:
D = bsxfun(#minus, A', B);
D(D < 0) = Inf;
[~, C] = min(D, [], 1);

matlab vector addition like multiplication, without for loop

Normally when one adds two vectors this is what happens
[1 4] + [2 5] = [3 9]
I want it to do this:
[1 4] + [2 5] = 3 6
6 9
So basically addition like how multiplication happens. But without using for-loops.
Thanks so much!
This is one of the poster cases for using bsxfun
x = [1 4];
y = [2 5];
bsxfun(#plus,x,y')
One way to do it is with meshgrid.
x = [1 4];
y = [2 5];
[a, b] = meshgrid(y,x);
a + b