How can I create a new matrix via replacement in matlab? - 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

Related

Matrix direct sum

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

Indexing a matrix in matlab according to conditions set on other matrices

I am trying to index my matrix based on two conditions, I'll explain.
Let's say I have two matrices:
a = [7 3 4; 5 6 7; 4 8 0];
b = [1 9 8; 2 4 6; 6 1 6];
And a third matrix to index:
c = [1 2 3; 4 5 6; 7 8 9];
My aim is to index c in a way that I get a 3x3 matrix in which only the values of c are copied over for whose indexes the following conditions are met and the rest are zeros.
a <= 5, b >= 6
Resulting matrix:
result = [0 2 3; 0 0 0; 7 0 9]
I hope I was able to explain my problem.
Given
a = [7 3 4; 5 6 7; 4 8 0];
b = [1 9 8; 2 4 6; 6 1 6];
c = [1 2 3; 4 5 6; 7 8 9];
result = zeros(size(c);
Using logical indexing,
>> d = (a <= 5) & (b >= 6)
d =
0 1 1
0 0 0
1 0 1
>> result(d) = c(d)
result =
0 2 3
0 0 0
7 0 9
Loop throw rows and columns and set the result.
for row=1:size(a,1)
for col=1:size(a,2)
if(a(row,col)> b(row,col))
result(row,col) = 0
else
result(row,col) = c(row,col)
end
end
end

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

Matlab vector to matrix conversion

I want to convert the following vector A into matrix B, best demonstrated by this example:
n = 4;
A = [1 2 3 4 5 6];
B = [ 1 2 3 4;
2 3 4 5;
3 4 5 6; ]
I am currently using a loop to achieve this and wondered if it was possible to vectorize it?
Thanks L.
You can use bsxfun -
A(bsxfun(#plus,[0:numel(A)-n]',1:n))
You can also use hankel -
hankel(A(1:n),A(n:end)).'
Sample run -
>> A = [3,4,6,0,1,2]
A =
3 4 6 0 1 2
>> n
n =
4
>> A(bsxfun(#plus,[0:numel(A)-n]',1:n))
ans =
3 4 6 0
4 6 0 1
6 0 1 2
>> hankel(A(1:n),A(n:end)).'
ans =
3 4 6 0
4 6 0 1
6 0 1 2
If you have the Signal Processing Toolbox you can also use convmtx:
n = 4;
A = [1 2 3 4 5 6];
m = numel(A)-n;
B = flipud(convmtx(A,m+1));
B = B(:,m+1:end-m);

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