How to overlap 2 matrices, where zeros are "transparent" - matlab

I have 2 matrices, say
A = [ 0 4 9 B = [ 0 0 2
0 2 1 1 6 1
3 0 0 ] 3 9 8 ]
I want the result to be "A overlapped with B".
Any elements of B which are 0 should be "transparent" and show through the value of A
All other elements should be the balue of B.
So I should get:
result = [ 0 4 2
1 6 1
3 9 8 ]

A=[ 0 4 9;
0 2 1;
3 0 0];
B=[ 0 0 2;
1 6 1;
3 9 8];
result = A;
result( B~=0 ) = B( B~=0 );

Related

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
>>

Finding the non-intersecting rows in a matrix

I want to find the non-intersecting rows in a large matrix. As an example:
A=[1 5 3; 3 4 5; 7 9 10;4 5 6;11 2 8; 3 5 10]
In this matrix, the non-intersecting rows are: [1 5 3], [11 2 8] and [7 9 10]. How can I program this in Matlab in a fast way?
If I may bsxfun -
M = squeeze(any(bsxfun(#eq,A,permute(unique(A),[3 2 1])),2))
[~,row_idx] = max(M,[],1)
out = A(sum(M,2).' == histc(row_idx,1:size(A,1)),:)
Sample step-by-step run -
A =
1 5 3
3 4 5
7 9 10
4 5 6
11 2 8
3 5 10
M =
1 0 1 0 1 0 0 0 0 0 0
0 0 1 1 1 0 0 0 0 0 0
0 0 0 0 0 0 1 0 1 1 0
0 0 0 1 1 1 0 0 0 0 0
0 1 0 0 0 0 0 1 0 0 1
0 0 1 0 1 0 0 0 0 1 0
row_idx =
1 5 1 2 1 4 3 5 3 3 5
out =
1 5 3
7 9 10
11 2 8
You can look for rows that adding them to union of previous rows increases the number of elements in the union by the number of columns (i.e. all elements in that row are new):
B = [];
C = zeros(1,size(A,1));
for k=1:size(A,1),
B1 = union(B, A(k,:));
C(k) = numel(B1)-numel(B);
B=B1;
end
result = A(C==size(A,2),:);

How to create block matrix from a matrix (the block) and a number (number of repetitions) in Matlab? [duplicate]

This question already has answers here:
MATLAB: Create a block diagonal matrix with same repeating block
(5 answers)
Closed 8 years ago.
Matlab syntax question :
Given some n x d matrix A(say [1 2 3; 4 5 6]), and a number k (say 2), how to create a block matrix in which A appears k times ( [ 1 2 3 0 0 0; 4 5 6 0 0 0 ; 0 0 0 1 2 3; 0 0 0 4 5 6] in my example) ?
Another example, if A is [1 2 3; 4 5 6] and k=3 then the output should be:
[ 1 2 3 0 0 0 0 0 0;
4 5 6 0 0 0 0 0 0;
0 0 0 1 2 3 0 0 0;
0 0 0 4 5 6 0 0 0;
0 0 0 0 0 0 1 2 3;
0 0 0 0 0 0 4 5 6
]
It is quit easy, just make a loop and play with the size of the matrix O:
A =[
1 2 3;
4 5 6]
O = zeros(size(A))
B = [A O; O A]
B =[
1 2 3 0 0 0;
4 5 6 0 0 0;
0 0 0 1 2 3;
0 0 0 4 5 6]
I hope ur looking for this.
clc
A =[
1 2 3;
4 5 6]
O = zeros(size(A))
B = [];
K = 3;
line = [];
for (i=1:K)
line = [];
for (j=1:K)
if (j==i)
line = [line A]
else
line =[line O]
end
end
B = [B; line];
end

count match value two matrix using bsxfun

I use c=bsxfun(#eq,b,a) to compare value of two matrix. but I find it difficult to count un-match values. for example, I use this code
a = [1 2 3 4 7 6; ...
3 2 4 6 7 2 ];
b = [1 3 2 4 5 7; ...
3 4 5 6 7 2; ...
2 3 4 5 6 6];
for i = 1:size(a,1)
c= bsxfun(#eq,a(i,:),b)
match = sum(c')
end
and result
c =
1 0 0 1 0 0
0 0 0 0 1 0
0 0 0 0 0 1
match =
2 1 1
c =
0 0 0 0 0 0
1 0 0 1 1 1
0 0 1 0 0 0
match =
0 4 1
I want to save value first match matrix with second match. for example
total_match =
2 5 2
Do you have any suggestion ? thanks..
No need for loop
match = bsxfun( #eq, permute( a, [1 3 2]), permute( b, [3 1 2] ) ); % result in 2x3x6 boolean
match = sum( match, 3 ); % sum matches across rows of a--b
total_match = sum( match, 1 );
PS
It is best not to use i and j as variable names in Matlab.

Replace duplicate elements from vector with 0 (Matlab/Octave)

I want to replace duplicate elements from a vector with 0, and keep only the first occurrence.
If I have a vector like
[ 1 1 2 2 2 3 3 3 4 4 4 4 5 5 5 5 6 6 6 ]
how could I transform it into
[ 1 0 2 0 0 3 0 0 4 0 0 0 5 0 0 0 6 0 0 ] ?
Thanks.
a = [ 1 1 2 2 2 3 3 3 4 4 4 4 5 5 5 5 6 6 6 ];
[c, ia] = unique(a, 'first');
t = a;
t(ia) = 0;
filtered_vect = a - t;
edit: That in a more concise way, destroying the original vector:
a = [ 1 1 2 2 2 3 3 3 4 4 4 4 5 5 5 5 6 6 6 ];
[c, ia] = unique(a, 'first');
a(~ismember(1:length(a),ia)) = 0;