MATLAB Matrix Transformation - matlab

I need to transform a matrix:
X = [1 2; 3 4]
X = 1 2
3 4
to
X = [1 2; 1 2; 1 2; 3 4; 3 4; 3 4]
X = 1 2
1 2
1 2
3 4
3 4
3 4
and do this operation for a matrix with any number of rows.
How can I achieve this in MATLAB?

Here is a nice and easy way to do this using kron
kron(X,[1 1 1]')
this produces
1 2
1 2
1 2
3 4
3 4
3 4

Related

Count repeating integers in an array

If I have this vector:
x = [1 1 1 1 1 2 2 2 3 4 4 6 6 6 6]
I would like to get the position of each unique number according to itself.
y = [1 2 3 4 5 1 2 3 1 1 2 1 2 3 4]
At the moment I'm using:
y = sum(triu(x==x.')) % MATLAB 2016b and above
It's compact but obviously not memory efficient.
For the pure beauty of MATLAB programming I would avoid using a loop. Do you have a better simple implementation ?
Context:
My final goal is to sort the vector x but with the constraint that a number that appear N times has the priority over another number that has appeared more than N times:
[~,ind] = sort(y);
x_relative_sort = x(ind);
% x_relative_sort = 1 2 3 4 6 1 2 4 6 1 2 6 1 6 1
Assuming x is sorted, here's one vectorized alternative using unique, diff, and cumsum:
[~, index] = unique(x);
y = ones(size(x));
y(index(2:end)) = y(index(2:end))-diff(index).';
y = cumsum(y);
And now you can apply your final sorting:
>> [~, ind] = sort(y);
>> x_relative_sort = x(ind)
x_relative_sort =
1 2 3 4 6 1 2 4 6 1 2 6 1 6 1
If you have positive integers you can use sparse matrix:
[y ,~] = find(sort(sparse(1:numel(x), x, true), 1, 'descend'));
Likewise x_relative_sort can directly be computed:
[x_relative_sort ,~] = find(sort(sparse(x ,1:numel(x),true), 2, 'descend'));
Just for variety, here's a solution based on accumarray. It works for x sorted and containing positive integers, as in the question:
y = cell2mat(accumarray(x(:), x(:), [], #(t){1:numel(t)}).');
You can be more memory efficient by only comparing to unique(x), so you don't have a large N*N matrix but rather N*M, where N=numel(x), M=numel(unique(x)).
I've used an anonymous function syntax to avoid declaring an intermediate matrix variable, needed as it's used twice - this can probably be improved.
f = #(X) sum(cumsum(X,2).*X); y = f(unique(x).'==x);
Here's my solution that doesn't require sorting:
x = [1 1 1 1 1 2 2 2 3 4 4 6 6 6 6 1 1 1];
y = cell2mat( splitapply(#(v){cumsum(v)},x,cumsum(logical([1 diff(x)]))) ) ./ x;
Explanation:
% Turn each group new into a unique number:
t1 = cumsum(logical([1 diff(x)]));
% x = [1 1 1 1 1 2 2 2 3 4 4 6 6 6 6 1 1 1];
% t1 = [1 1 1 1 1 2 2 2 3 4 4 5 5 5 5 6 6 6];
% Apply cumsum separately to each group:
t2 = cell2mat( splitapply(#(v){cumsum(v)},x,t1) );
% t1 = [1 1 1 1 1 2 2 2 3 4 4 5 5 5 5 6 6 6];
% t2 = [1 2 3 4 5 2 4 6 3 4 8 6 12 18 24 1 2 3];
% Finally, divide by x to get the increasing values:
y = t2 ./ x;
% x = [1 1 1 1 1 2 2 2 3 4 4 6 6 6 6 1 1 1];
% t2 = [1 2 3 4 5 2 4 6 3 4 8 6 12 18 24 1 2 3];

Combination of Matrices

I have two matrices, one of which (Let's say matrix H) is 4x2 and the other one (matrix N) is 100x2.
I want to make a combination for every pair of N, containing every pair of H.
Basically, if my
H = [2 2; 2 4; 4 2; 4 4]
N = [1 1; 1 2; 1 3; ...;
10 8; 10 9; 10 10]
I would like to have a final matrix
M = [1 2 2 1; 1 2 4 1; 1 4 2 1; 1 4 4 1; 1 2 2 2; 1 2 4 2; ...; 10 4 4 10]
of a size 100x4 (because every pair of N will be multiplied |H|=4 times.)
So all the pairs of H matrix will be between all pairs of my N matrix.
I hope I am clear enough.
Use the follwing syntax:
%calculates the Cartesian multipication of 1:size(h,1) and 1:size(N,1)
sets = {1:size(H,1), 1:size(N,1)};
[hInds, nInds] = ndgrid(sets{:});
%generates the output matrix
outRes = [N( nInds(:),1),H( hInds(:),1),H( hInds(:),2),N( nInds(:),2)];
Partial results (displaying just the first rows of the output):
outRes =
1 2 2 1
1 2 4 1
1 4 2 1
1 4 4 1
1 2 2 2
1 2 4 2
1 4 2 2
1 4 4 2
1 2 2 3
1 2 4 3
1 4 2 3
1 4 4 3
...
Notice that if N is 4x2 and N is 10x2, the final matrix size will be 40x4 and not 100x4 as you mentioned.
Try this:
H= [2 2; 2 4; 4 2; 4 4];
N= fix(100*(rand(10,2))) % Replace this with your N matrix
iter=0;
for i=1:10
for j=1:4
iter=iter+1;
A(iter,:)=[N(i,1), H(j,1:2), N(i,2)];
end
end
A

Fill diagonals of matrix according to vector

I'd like to transform a vector
v = [1,2,3,4]
into a matrix (4x4) with diagonals like this:
1 2 3 4
2 1 2 3
3 2 1 2
4 3 2 1
You want to generate a Toeplitz matrix, so you should use the toeplitz function.
v = [ 1 2 3 4 ];
out = toeplitz(v)
out =
1 2 3 4
2 1 2 3
3 2 1 2
4 3 2 1
If A is your 4x4 array, just do:
A(1:5:end) = 1:4;
Best
v = [1 2 3 4];
A = gallery('circul',v);
B = triu(A,0)+triu(A,1)';

Creating an index matrix depending on Reference matrix and matrix of Data matlab

given matrix A of size 6 by 6 contain blocks of numbers,each block of size 2 by 2, and outher reference matrix R of size 2 by 12 also contain blocks of numbers, each block of size 2 by 2. the perpse of the whole process is to form a new matrix, called the Index matrix, contain index's that refer to the position of the blocks within the matrix A based on the order of the blocks within the reference matrix R. and here is an exemple
matrix A:
A =[1 1 2 2 3 3;
1 1 2 2 3 3;
1 1 3 3 4 4;
1 1 3 3 4 4;
4 4 5 5 6 6;
4 4 5 5 6 6 ]
matrix R:
R=[1 1 2 2 3 3 4 4 5 5 6 6;
1 1 2 2 3 3 4 4 5 5 6 6 ]
the new matrix is:
Index =[1 2 3;
1 3 4;
4 5 6]
any ideas ?
With my favourite three guys - bsxfun, permute, reshape for an efficient and generic solution -
blksz = 2; %// blocksize
num_rowblksA = size(A,1)/blksz; %// number of blocks along rows in A
%// Create blksz x blksz sized blocks for A and B
A1 = reshape(permute(reshape(A,blksz,num_rowblksA,[]),[1 3 2]),blksz^2,[])
R1 = reshape(R,blksz^2,1,[])
%// Find the matches with "bsxfun(#eq" and corresponding indices
[valid,idx] = max(all(bsxfun(#eq,A1,R1),1),[],3)
%// Or with PDIST2:
%// [valid,idx] = max(pdist2(A1.',reshape(R,blksz^2,[]).')==0,[],2)
idx(~valid) = 0
%// Reshape the indices to the shapes of blocked shapes in A
Index = reshape(idx,[],num_rowblksA).'
Sample run with more random inputs -
>> A
A =
2 1 1 2
1 2 2 1
1 1 1 1
2 2 2 2
1 2 2 1
1 2 1 1
>> R
R =
2 1 1 1 1 2 2 2 1 1 1 1
2 1 2 1 1 2 2 1 2 2 2 1
>> Index
Index =
0 0
5 5
3 0

Is it possible to delete elements from matlab matrix?

I have a matlab matrix in the form
A = [1 2 3 4 5 6; 1 1 2 2 3 3; 1 2 3 3 2 1]
Is it possible to delete the 4th, 5th and 6th columns of the matrix A so that the result would be:
A = [1 2 3; 1 1 2; 1 2 3]
This would do it:
A(:,4:6)=[];
octave-3.4.0:1> A = [1 2 3 4 5 6; 1 1 2 2 3 3; 1 2 3 3 2 1]
A =
1 2 3 4 5 6
1 1 2 2 3 3
1 2 3 3 2 1
octave-3.4.0:2> A = A(:,1:3)
A =
1 2 3
1 1 2
1 2 3