Matrix direct sum - matlab

Is there a function in MATLAB that allows us to do matrix direct sum? For example,
A = [1 2 3
3 4 5]
B = [5 6
7 8
9 8]
and we want the direct sum A ⊕ B that gives us:
directSum(A,B) = [1 2 3 0 0
3 4 5 0 0
0 0 0 5 6
0 0 0 7 8
0 0 0 9 8]
If there is not, what are some quick ways to implement this?

Use blkdiag to compute the direct sum of matrices:
A = [1 2 3; 3 4 5];
B = [5 6; 7 8; 9 8];
blkdiag(A, B)
% ans = 5x5
%
% 1 2 3 0 0
% 3 4 5 0 0
% 0 0 0 5 6
% 0 0 0 7 8
% 0 0 0 9 8

Related

Reorder the elements in each row of a square diagonal matrix in Matlab

I have a square symmetric matrix A of dimension n in Matlab and I want to reorder the elements in each row in ascending order but preserving the symmetry and without touching the diagonal elements.
E.g.
n=4;
A=[10 9 8 7; 9 6 5 4; 8 5 3 2; 7 4 2 1];
%reorder A in order to obtain
B=[10 7 8 9; 7 6 4 5; 8 4 3 2; 9 5 2 1];
Could you provide some help?
you can use triu to sort only the upper triangle and then add the transpose to keep the symmetry. finally just set the diagonal as in the original matrix:
n=4;
A=[10 9 8 7; 9 6 5 4; 8 5 3 2; 7 4 2 1];
% upper triangle indexes
UI = triu(true(size(A)),1);
% upper triangle of A
B = triu(A,1);
% assign inf to diagonal and below - important if there are any negative values in A
B(~UI) = -inf;
% sort rows descending order
B = sort(B,2,'ascend');
% set infs to 0
B(isinf(B)) = 0;
% add lower triangle matrix
B = B + B.';
% set diagonal as original A
B(1:n+1:n^2) = diag(A)
A = [10 9 8 7;
9 6 5 4;
8 5 3 2;
7 4 2 1];
B = sort(triu(A,1),2) + diag(diag(A)) + sort(triu(A,1),2)';
Explain
triu(A,1) gets the upper triangular matrix above the main diagonal, that is
ans0 =
0 9 8 7
0 0 5 4
0 0 0 2
0 0 0 0
sort(triu(A,1),2) sorts the elements in each row of the above result in an ascending order, that is
ans1 =
0 7 8 9
0 0 4 5
0 0 0 2
0 0 0 0
diag(diag(A)) gets the diagonal of A, that is
ans2 =
10 0 0 0
0 6 0 0
0 0 3 0
0 0 0 1
sort(triu(A,1),2)' gets the transpose of the sorted upper triangular matrix, that is
ans =
0 0 0 0
7 0 0 0
8 4 0 0
9 5 2 0
Add ans1, ans2 and ans3 up, you will get B.

All scaled combination of vector elements in Matlab

I have a vector like this:
A=[3 4 5 6];
I would like obtain a new Matrix B which is composed by all possible scaled combination of A elements avoiding rows with only one element (then at least two elements for each row), for instance:
B=[3 4 5 6;
3 4 5 0;
3 4 0 0;
0 4 5 6;
0 0 5 6;
3 0 5 6;
3 0 5 0;
0 0 5 6;
3 4 0 6;
0 4 0 6;
3 4 0 0;
etc...
];
Could you please help me?
Thanks in advance
Here's a way to do it:
A = [3 4 5 6]; % data
N = 2; % minimum number of elements that should be present
p = dec2bin(1:2^numel(A)-1)-'0'; % binary pattern. Each row is a combination
s = sum(p,2)>=N; % index to select rows of p that have at least N ones
result = bsxfun(#times, A, p(s,:)); % multiply with singleton expansion
This gives, in your example,
result =
0 0 5 6
0 4 0 6
0 4 5 0
0 4 5 6
3 0 0 6
3 0 5 0
3 0 5 6
3 4 0 0
3 4 0 6
3 4 5 0
3 4 5 6

How can I create a new matrix via replacement in matlab?

I have two matrix:
A=[1 2 3; 4 5 6; 7 8 9]
C=[0 0 2; 0 0 1; 0 0 8]
I want to keep nonzero values of C and create D. Then, replace the zero elements with A matrix.
So D should be:
D=[1 2 2; 4 5 1; 7 8 8]
I try this code:
A=[1 2 3; 4 5 6; 7 8 9]
C=[0 0 2; 0 0 1; 0 0 8]
T=A(C==0)
R=sparse(T)
K=find(sparse(C))
It didn't work
Use:
D = A;
D(C~=0) = C(C~=0);
drorco's answer is the right way to do it, but there is a one-liner that I couldn't resist:
D = ~C.*A + ~~C.*C;
A=[1 2 3; 4 5 6; 7 8 9];
C=[0 0 2; 0 0 1; 0 0 8];
D=A;
inds=find(C~=0);
D(inds)=C(inds)
D =
1 2 2
4 5 1
7 8 8

separation of rows with zero on its 1st column in matlab

how can separate my matrix like this
A=[0 1 1 4; 1 2 0 8; 0 3 0 5; 2 3 0 4; 2 4 0 3; 3 4 0 2]
my reference is the 1st column. that if the number in the first column is zero i will segregate it like this:
B=[0 1 1 4; 0 3 0 5]
and C=[1 2 0 8; 2 3 0 4; 2 4 0 3; 3 4 0 2]
You can codegolf this but essentially the machinery would be around this type of indexing with a conditional.
>> A=[0 1 1 4; 1 2 0 8; 0 3 0 5; 2 3 0 4; 2 4 0 3; 3 4 0 2];
>> bool = A(:,1)==0;
>> ind = 1:size(A,1);
>> B = A(ind(bool),:);
>> C = A(ind(~bool),:);
>> B
B =
0 1 1 4
0 3 0 5
>> C
C =
1 2 0 8
2 3 0 4
2 4 0 3
3 4 0 2
>>

Ismember by row in MATLAB

I'm trying to figure out a matrix-oriented way to perform the ismember function by row in MATLAB. That is, if I have matrices
[1 2 3 4 5 6]
[7 8 9 10 11 12]
And I put in
[3 4 5]
[10 11 12]
Into some ismember-ish function, I'd like it to return
[0 0 1 1 1 0]
[0 0 0 1 1 1]
Other than looping over each row of the matrix in a for loop, is there a way to do this?
Assuming that your data are available as matrices A and B
A = [
1 2 3 4 5 6
7 8 9 10 11 12
];
B = [
3 4 5
10 11 12];
you can convert them to cells and then use cellfun
cellA = mat2cell(A, ones(1, size(A,1)), size(A,2));
cellB = mat2cell(B, ones(1, size(B,1)), size(B,2));
membership = cell2mat(cellfun(#ismember, cellA, cellB, 'UniformOutput', false));
This returns
membership =
0 0 1 1 1 0
0 0 0 1 1 1
A = [5 3 4 2]; B = [2 4 4 4 6 8];
[Lia1,Locb1] = ismember(A,B)
Lia1 =
1 1 1 1 0 0
Locb1 =
4 3 3 3 0 0