Block diagonal matrix from columns - arrays

Suppose I have an m x n matrix A .
Is there a way to create B, a (n x m) x n matrix whose "diagonal" is formed by A's columns ?
Example:
A = [1 2;
3 4]
B = [1 0;
3 0;
0 2;
0 4]

Here is a way:
Convert A to a cell array of its columns, using mat2cell;
From that cell array generate a comma-separated list, and use it as an input to blkdiag.
Code:
A = [1 2; 3 4]; %// example data
C = mat2cell(A, size(A,1), ones(1,size(A,2))); %// step 1
B = blkdiag(C{:}); %// step 2
This produces
B =
1 0
3 0
0 2
0 4

Here is a short script to accomplish this. It works for any dimensions of A.
A=[1 2; 3 4];
[R C] = size(A);
for i=1:C
B( 1+R*(i-1) : R*i , i ) = A(:,i);
end

Related

Get the first 2 non-zero elements from every row of matrix

I have a matrix A like this:
A = [ 1 0 2 4; 2 3 1 0; 0 0 3 4 ]
A has only unique row elements except zero, and each row has at least 2 non-zero elements.
I want to create a new matrix B from A,where each row in B contains the first two non-zero elements of the corresponding row in A.
B = [ 1 2 ; 2 3 ; 3 4 ]
It is easy with loops but I need vectorized solution.
Here's a vectorized approach:
A = [1 0 2 4; 2 3 1 0; 0 0 3 4]; % example input
N = 2; % number of wanted nonzeros per row
[~, ind] = sort(~A, 2); % sort each row of A by the logical negation of its values.
% Get the indices of the sorting
ind = ind(:, 1:N); % keep first N columns
B = A((1:size(A,1)).' + (ind-1)*size(A,1)); % generate linear index and use into A
Here is another vectorised approach.
A_bool = A > 0; A_size = size(A); A_rows = A_size(1);
A_boolsum = cumsum( A_bool, 2 ) .* A_bool; % for each row, and at each column,
% count how many nonzero instances
% have occurred up to that column
% (inclusive), and then 'zero' back
% all original zero locations.
[~, ColumnsOfFirsts ] = max( A_boolsum == 1, [], 2 );
[~, ColumnsOfSeconds ] = max( A_boolsum == 2, [], 2 );
LinearIndicesOfFirsts = sub2ind( A_size, [1 : A_rows].', ColumnsOfFirsts );
LinearIndicesOfSeconds = sub2ind( A_size, [1 : A_rows].', ColumnsOfSeconds );
Firsts = A(LinearIndicesOfFirsts );
Seconds = A(LinearIndicesOfSeconds);
Result = horzcat( Firsts, Seconds )
% Result =
% 1 2
% 2 3
% 3 4
PS. Matlab / Octave common subset compatible code.

Matlab: Assemble submatrices whose #cols and #rows are stored in a vector

I have two vectors, R and C, which have the number of rows and columns, respectively, of submatrices that I need to assemble in a ones matrix I (40x20). There's 12 submatrices total.
R = [4 2 4 4 2 4];
C = [4 16 16 4];
Moreover, all the elements of each submatrix have its value stored in vector k:
k = [3 2 3 3 2 3 2 1 2 2 1 2 2 1 2 2 1 2 3 2 3 3 2 3 ]; % 24 elements
Thus for instance, submatrix M(1:4,1:4) has 4 rows, and 4 columns and value equal to k(1) = 1.
QUESTION: How can I assemble matrix M with all submatrices?
Any ideas?
Thanks!
EDIT:
The matrix M should look like this:
and the submatrices:
and the values of k:
Here is a vectorized solution:
R1 = repelem(1:numel(R), R);
C1 = repelem(1:numel(C), C);
[CC RR] = meshgrid(C1, R1);
idx = sub2ind([numel(R), numel(C)], RR, CC);
result = k(idx);
Instead you can use cell array, fill it with sub matrices and then convert the cell array to a matrix.
carr = cell(numel(R), numel(C));
k1 = reshape(k,numel(R),numel(C));
for ii = 1:numel(R)
for jj = 1:numel(C)
carr(ii,jj)=repmat(K1(ii,jj), R(ii), C(jj));
end
end
result = cell2mat(carr)

MATLAB intersection of 3d arrays

I have two 3d arrays:
A(:,:,1) = [1 2 3; 4 5 6; 7 8 9];
A(:,:,2) = [1 0 0; 0 1 0; 0 0 1];
A(:,:,3) = [3 2 1; 6 5 4; 9 8 7];
...
and
B(:,:,1) = [1 1 1; 2 2 2; 3 3 3];
B(:,:,2) = [1 0 0; 0 1 0; 0 0 1];
B(:,:,3) = [3 3 3; 2 2 2; 1 1 1];
...
They both consist of 3x3 matrices and their third dimensions are very large. I want to obtain the array of matrices that exist in both arrays. I am doing it in a for loop by comparing element-wise (matrix-wise). It takes a very long time, so I am looking for an easier way (or an existing function) to do the same.
Thanks!
Collapse the first two dimensions into one and transpose, so that matrices of the 3D array become rows of a matrix. That way you can use intersect(...,'rows'). Finally, transpose back and reshape back:
[m, n, p] = size(A);
result = intersect(reshape(A, [], p).', reshape(B, [], p).', 'rows');
result = reshape(result.', m, n, []);

Create all possible Mx1 vectors from an Nx1 vector in MATLAB

I am trying to create all possible 1xM vectors (word) from a 1xN vector (alphabet) in MATLAB. N is > M. For example, I want to create all possible 2x1 "words" from a 4x1 "alphabet" alphabet = [1 2 3 4];
I expect a result like:
[1 1]
[1 2]
[1 3]
[1 4]
[2 1]
[2 2]
...
I want to make M an input to my routine and I do not know it beforehand. Otherwise, I could easily do this using nested for-loops. Anyway to do this?
Try
[d1 d2] = ndgrid(alphabet);
[d2(:) d1(:)]
To parameterize on M:
d = cell(M, 1);
[d{:}] = ndgrid(alphabet);
for i = 1:M
d{i} = d{i}(:);
end
[d{end:-1:1}]
In general, and in languages that don't have ndgrid in their library, the way to parameterize for-loop nesting is using recursion.
[result] = function cartesian(alphabet, M)
if M <= 1
result = alphabet;
else
recursed = cartesian(alphabet, M-1)
N = size(recursed,1);
result = zeros(M, N * numel(alphabet));
for i=1:numel(alphabet)
result(1,1+(i-1)*N:i*N) = alphabet(i);
result(2:M,1+(i-1)*N:i*N) = recursed; % in MATLAB, this line can be vectorized with repmat... but in MATLAB you'd use ndgrid anyway
end
end
end
To get all k-letter combinations from an arbitrary alphabet, use
n = length(alphabet);
aux = dec2base(0:n^k-1,n)
aux2 = aux-'A';
ind = aux2<0;
aux2(ind) = aux(ind)-'0'
aux2(~ind) = aux2(~ind)+10;
words = alphabet(aux2+1)
The alphabet may consist of up to 36 elements (as per dec2base). Those elements may be numbers or characters.
How this works:
The numbers 0, 1, ... , n^k-1 when expressed in base n give all groups of k numbers taken from 0,...,n-1. dec2base does the conversion to base n, but gives the result in form of strings, so need to convert to the corresponding number (that's part with aux and aux2). We then add 1 to make the numbers 1,..., n. Finally, we index alphabet with that to use the real letters of numbers of the alphabet.
Example with letters:
>> alphabet = 'abc';
>> k = 2;
>> words
words =
aa
ab
ac
ba
bb
bc
ca
cb
cc
Example with numbers:
>> alphabet = [1 3 5 7];
>> k = 2;
>> words
words =
1 1
1 3
1 5
1 7
3 1
3 3
3 5
3 7
5 1
5 3
5 5
5 7
7 1
7 3
7 5
7 7
use ndgrid function in Matlab
[a,b] = ndgrid(alphabet)

How to compare each element with each element of cell array without a two loops?

I have a two cell arrays R and C (two vectors with R(n-elements), C(m-elements)) and my task is to compare each element of R with each element of R and each element of C with each element of C. Comparison is to finding intersection of two cells. In result I want to obtain two matrices. One matrix Q for R nxn, where in cell Q(i,j) is intersection of two elements R(i) and R(j) and second matrix P for C mxm, where in cell P(i,j) is intersection of two elements C(i) and C(j).
Generally I can do this using two for-loops, but my data is quite big and I wonder if there is any method to speed up the computation?
The first idea was to replace the cell array, where in each cell are the indexes of rows (vector R) or columns (vector C) which I want to compare (rows and columns of binary matrix BM, BM is input data) . So If R(1) = {2 3 4}, and BM is 5x5, then R(1,:)=[0 1 1 1 0]. Now having this binary matrix R I could compare each row with each row only with one loop. But then I still need to come back to number of rows eg
R(1,:) = [0 1 1 1 0];
R(2,:) = [0 1 1 0 0]; %then
Q(1,2) = [0 1 1 0 0]; %(intersection of element R(1) and R(2)) and
C(1,:) = [1 1 0 0 0];
C(2,:) = [1 0 0 1 0]; %then
P(1,2) = [1 0 0 0 0]; % Now I want to obtain
Results(i,j) = sum(BM(Q(1,2),P(1,2)))=sum(BM([2 3],[1]));
Do you have any idea how to cope with this, and compare two vectors of cell array without a two loops?
Since Q( k, l ) is a vector with numCols (5 in your example) it cannot be stored in a 2D matrix Q: Q should either be a 2D cell array, or a 3D matrix.
Using the binary matrix directly to obtain Q (row intersections):
>> Q = bsxfun( #times, permute( BM, [1 3 2] ), permute( BM, [3 1 2] ) );
Now, Q( k, l, : ) holds the intersection betwee the k-th and l-th rows of BM.
Same goes for P:
>> P = bsxfun( #times, permute( BM, [3 2 1] ), permute( BM, [2 3 1] ) );

Resources