MATLAB concatenate 2D matrix tiles - matlab

I have n2 equally sized (8x8) matrices which I want to tile into a single matrix like in the following diagram:
I know I could concatenate them column by column and then concatenate each row, but I want to know if there's a simpler method to achieve this.

There's a simpler method, you can store all your matrices in a cell array, then reshape and convert back to a matrix:
In the following example, suppose that C is your n2×1 cell array of matrices:
cell2mat(reshape(C, sqrt(numel(C)), []));
The result is a single tiled matrix A as required.
Example
a = ones(2); b = 2 * a; c = 3 * a; d = 4 * a;
C = {a, b, c, d};
A = cell2mat(reshape(C, sqrt(numel(C)), []))
The result is:
A =
1 1 3 3
1 1 3 3
2 2 4 4
2 2 4 4
Note the order of the sub-matrices: they are arranged column-wise. If you want A to be:
A =
1 1 2 2
1 1 2 2
3 3 4 4
3 3 4 4
then you'll have to pass the transposed version of C to reshape:
cell2mat(reshape(C', sqrt(numel(C)), []))

If you already have a for loop where you create the 8-by-8 matrices, you can do something like this:
M = 8; % Rows of each block matrix
N = 8; % Columns of each square block matrix
m = 2; % Number of blocks across
n = 2; % Number of blocks vertically
A(m*n*M,N) = 0; % Preallocate an m*n*M-by-N column of blocks
for i = 1:m*n
a = rand(M,N); % Create your data, just random here
A(1+M*(i-1):M*i,:) = a; % Insert data
end
A = reshape(A,[M*m N*n]); % Reshape to obtain block matrix
This assumes that you have a single for loop iterating over all n^2 (or m*n) cases. Also, it builds up A one column of blocks at a time. Note: if you need to build it with the blocks going across the rows first, then you'll need to change the allocation of A and how the data is inserted by swapping the indices.

Yes there is!
%Assuming your matrices are A1, A2, A3 and A4:
A = zeros(size(A1)*2);
A(1:8,1:8) = A1;
A(9:16, 1:8) = A2;
A(1:8, 9:16) = A3;
A(9:16, 9:16) = A4;

Related

Create a matrix with alternate rows from two matrices [MATLAB]

I have the following question:
Write a program that:
Starts by requesting an A matrix by keyboard.
Next, if the number of columns of A is odd, the last column of additional zeros must be added. From this moment matrix, A has an even number (n) of columns.
The program will divide the input matrix into two sub-matrices.
The first sub-matrix (A1) contains the first n / 2 columns of A. The second sub-matrix (A2) has the last n / 2 columns.
Finally, the program must calculate and write on the screen a matrix B that has the rows of A1 in the odd rows and those of A2 in the pairs.
Example code
A = input('Enter a matrix:')
% A = magic(5) % for example
[filA, colA] = size(A);
if rem(colA,2)==1
A = [A, zeros(filA,1)]
colA = colA + 1;
end
A1 = A(:, [1:colA/2])
A2 = A(:, [1+(colA/2):colA])
%B
Here is the solution I propose you:
A = [1 2 3; 1 2 3; 1 2 3; 1 2 3];
[A_r,A_c] = size(A);
if (mod(A_c,2) ~= 0)
A = [A zeros(A_r,1)];
A_c = A_c + 1;
end
off = A_c / 2;
A1 = A(:,1:off);
A2 = A(:,(off+1):A_c);
B = reshape([A1(:) A2(:)].',2*A_r,[])
It makes use of the reshape function for interleaving the rows of the matrices A1 and A2. By omitting the ; at the end of the last line, you let Matlab print the output of the final computation in the console, which is:
B =
1 2
3 0
1 2
3 0
1 2
3 0
1 2
3 0
Using a step by step debugging approach, you can see how every step is being performed.
My solution
clear all, clc;
A = input('Ingrese una matriz:')
[filA, colA] = size(A);
if rem(colA,2)==1
A = [A, zeros(filA,1)]
colA = colA + 1;
end
A1 = A(:, [1:colA/2])
A2 = A(:, [1+(colA/2):colA])
B = A2([1;1]*(1:size(A2,1)),:)
B(1:2:end,:) = A1

Sort elements of rows in a matrix with another matrix

I have a matrix D of distances between 3 places and 4 persons
example D(2,3) = 10 means person 3 is far away from place 2 of 10 units.
D=[23 54 67 32
32 5 10 2
3 11 13 5]
another matrix A with the same number of rows (3 places) and where A(i,:) correspond to the persons that picked place i
example for place 1, persons 1 and 3 picked it
no one picked place 2
and persons 2 and 4 picked place 3
A=[1 3 0
0 0 0
2 4 0]
I want to reorder each row of A by the persons who are closest to the place it represents.
In this example, for place 1, person 1 is closer to it than person 3 based on D so nothing to do.
nothing to do for place 2
and there is a change for place 3 since person 4 is closer than 2 to place 3 D(3,2)>D(3,4)
The result should be
A=[1 3
0 0
4 2 ]
each row(place) in A can have 0 or many non zeros elements in it (persons that picked it)
Basically, I want to reorder elements in each row of A based on the rows of D (the closest to the location comes first), something like this but here A and D are not of the same size (number of columns).
[SortedD,Ind] = sort(D,2)
for r = 1:size(A,1)
A(r,:) = A(r,Ind(r,:));
end
There is another Matlab function sortrows(C,colummn_index) that can do the trick. It can sort rows based on the elements in a particular column. So if you transpose your matrix A (C = A') and extend the result by adding to the end the proper column, according to which you want to sort a required row, then you will get what you want.
To be more specific, you can do something like this:
clear all
D=[23 54 67 32;
32 5 10 2;
3 11 13 5];
A=[1 0;
3 0;
4 2 ];
% Sort elements in each row of the matrix A,
% because indices of elements in each row of the matrix D are always
% ascending.
A_sorted = sort(A,2);
% shifting all zeros in each row to the end
for i = 1:length(A_sorted(:,1))
num_zeros = sum(A_sorted(i,:)==0);
if num_zeros < length(A_sorted(i,:))
z = zeros(1,num_zeros);
A_sorted(i,:) = [A_sorted(i,num_zeros+1:length(A_sorted(i,:))) z];
end;
end;
% Prelocate in memory an associated array of the corresponding elements in
% D. The matrix Dr is just a reduced derivation from the matrix D.
Dr = zeros(length(A_sorted(:,1)),length(A_sorted(1,:)));
% Create a matrix Dr of elements in D corresponding to the matrix A_sorted.
for i = 1:length(A_sorted(:,1)) % i = 1:3
for j = 1:length(A_sorted(1,:)) % j = 1:2
if A_sorted(i,j) == 0
Dr(i,j) = 0;
else
Dr(i,j) = D(i,A_sorted(i,j));
end;
end;
end;
% We don't need the matrix A_sorted anymore
clear A_sorted
% In order to use the function SORTROWS, we need to transpose matrices
A = A';
Dr = Dr';
% The actual sorting procedure starts here.
for i = 1:length(A(1,:)) % i = 1:3
C = zeros(length(A(:,1)),2); % buffer matrix
C(:,1) = A(:,i);
C(:,2) = Dr(:,i);
C = sortrows(C,2);
A(:,i) = C(:,1);
% shifting all zeros in each column to the end
num_zeros = sum(A(:,i)==0);
if num_zeros < length(A(:,i))
z = zeros(1,num_zeros);
A(:,i) = [A(num_zeros+1:length(A(:,i)),i) z]';
end;
end;
% Transpose the matrix A back
A = A';
clear C Dr z

Permuting columns of a matrix in MATLAB

Say I have an n by d matrix A and I want to permute the entries of some columns. To do this, I compute permutations of 1 ... n as
idx1 = randperm(n)'
idx2 = randperm(n)'
Then I could do:
A(:,1) = A(idx1,1)
A(:,2) = A(idx2,2)
However, I dont want to do this using a for-loop, as it'll be slow. Say I have an n by d matrix A and an n by d index matrix IDX that specifies the permutations, is there a quicker equivalent of the following for-loop:
for i = 1:d
A(:,i) = A(IDX(:,i),i);
end
Using linear-indexing with the help of bsxfun -
[n,m] = size(A);
newA = A(bsxfun(#plus,IDX,[0:m-1]*n))
I guess another rather stupid way to do it is with cellfun, stupid because you have to convert it into a cell and then convert it back, but it is there anyways.
N=ones(d,1)*n; %//create a vector of d length with each element = n
M=num2cell(N); %//convert it into a cell
P=cellfun(#randperm, M,'uni',0); %//cellfun applys randperm to each cell
res = cell2mat(P); %//Convert result back into a matrix (since results are numeric).
This also allows randperm of type Char and String, but the cell2mat will not work for those cases, results are in Cell Array format instead.
for d = 5, n = 3:
>> res =
1 3 2
1 2 3
2 3 1
3 1 2
3 2 1

Changing the size of a matrix in MATLAB

I am given the following matrices A of size 3x1 and B of size 5x1
A = B=
1 A
2 B
3 C
D
E
I want to convert matrix C in a 15x2 matrix
C =
1 A
1 B
1 C
1 D
1 E
2 A
.
.
.
3 E
How can I make it?
Can be done with repmat
D = repmat(A',size(B,1),1);
C = [D(:),repmat(B,size(A,1),1)]
Here's a different alternative based on code for generating truth tables from Generate All Possible combinations of a Matrix in Matlab
ind = dec2base(0:power(5,2)-1,5)-47;
C = [A(ind(1:15,1) + 48, B(ind(1:15,2)];
And if you want to generalize it
m = max(size(A,1),size(B,1));
n = size(A,1)*size(B,1);
col = 2;
ind = dec2base(0:power(n,col)-1,n)-47;
ind = ind(1:n,:);
C = [A(ind(:,1) + 48, B(ind(:,2)];
The + 48 is just to convert your A matrix from a numerical matrix to a char matrix so that C can hold both number and letters. You can leave it out if A was already a char matrix.
What's useful about this technique is that by changing col, this generalizes to combing more than just 2 vectors in a similar fashion

Vector of the occurence number

I have a vector a=[1 2 3 1 4 2 5]'
I am trying to create a new vector that would give for each row, the occurence number of the element in a. For instance, with this matrix, the result would be [1 1 1 2 1 2 1]': The fourth element is 2 because this is the first time that 1 is repeated.
The only way I can see to achieve that is by creating a zero vector whose number of rows would be the number of unique elements (here: c = [0 0 0 0 0] because I have 5 elements).
I also create a zero vector d of the same length as a. Then, going through the vector a, adding one to the row of c whose element we read and the corresponding number of c to the current row of d.
Can anyone think about something better?
This is a nice way of doing it
C=sum(triu(bsxfun(#eq,a,a.')))
My first suggestion was this, a not very nice for loop
for i=1:length(a)
F(i)=sum(a(1:i)==a(i));
end
This does what you want, without loops:
m = max(a);
aux = cumsum([ ones(1,m); bsxfun(#eq, a(:), 1:m) ]);
aux = (aux-1).*diff([ ones(1,m); aux ]);
result = sum(aux(2:end,:).');
My first thought:
M = cumsum(bsxfun(#eq,a,1:numel(a)));
v = M(sub2ind(size(M),1:numel(a),a'))
on a completely different level, you can look into tabulate to get info about the frequency of the values. For example:
tabulate([1 2 4 4 3 4])
Value Count Percent
1 1 16.67%
2 1 16.67%
3 1 16.67%
4 3 50.00%
Please note that the solutions proposed by David, chappjc and Luis Mendo are beautiful but cannot be used if the vector is big. In this case a couple of naïve approaches are:
% Big vector
a = randi(1e4, [1e5, 1]);
a1 = a;
a2 = a;
% Super-naive solution
tic
x = sort(a);
x = x([find(diff(x)); end]);
for hh = 1:size(x, 1)
inds = (a == x(hh));
a1(inds) = 1:sum(inds);
end
toc
% Other naive solution
tic
x = sort(a);
y(:, 1) = x([find(diff(x)); end]);
y(:, 2) = histc(x, y(:, 1));
for hh = 1:size(y, 1)
a2(a == y(hh, 1)) = 1:y(hh, 2);
end
toc
% The two solutions are of course equivalent:
all(a1(:) == a2(:))
Actually, now the question is: can we avoid the last loop? Maybe using arrayfun?