Finding the non-intersecting rows in a matrix - matlab

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),:);

Related

Block matrix creation

I have two matrices A and D with sizes mxm. I want to create with these two a block matrix B size mn x mn . For example if n=5 then the output will be
B= D A A A A
A D 0 0 0
A 0 D 0 0
A 0 0 D 0
A 0 0 0 D
I have managed to create this form with many for loops but I would like a quicker solution with functions provided by matlab.
This should do the trick:
m = 3;
n = 5;
mn = m*n;
A_val = 4;
D_val = 2;
% Just an example, you could use rand(m) instead...
A = repmat(A_val,m);
D = repmat(D_val,m);
D_cell = repmat({D},1,n);
B = blkdiag(D_cell{:});
idx_1 = 1:m;
idx_2 = (m+1):mn;
B(idx_2,idx_1) = repmat(A,n-1,1);
B(idx_1,idx_2) = repmat(A,1,n-1);
Output:
B =
2 2 2 4 4 4 4 4 4 4 4 4 4 4 4
2 2 2 4 4 4 4 4 4 4 4 4 4 4 4
2 2 2 4 4 4 4 4 4 4 4 4 4 4 4
4 4 4 2 2 2 0 0 0 0 0 0 0 0 0
4 4 4 2 2 2 0 0 0 0 0 0 0 0 0
4 4 4 2 2 2 0 0 0 0 0 0 0 0 0
4 4 4 0 0 0 2 2 2 0 0 0 0 0 0
4 4 4 0 0 0 2 2 2 0 0 0 0 0 0
4 4 4 0 0 0 2 2 2 0 0 0 0 0 0
4 4 4 0 0 0 0 0 0 2 2 2 0 0 0
4 4 4 0 0 0 0 0 0 2 2 2 0 0 0
4 4 4 0 0 0 0 0 0 2 2 2 0 0 0
4 4 4 0 0 0 0 0 0 0 0 0 2 2 2
4 4 4 0 0 0 0 0 0 0 0 0 2 2 2
4 4 4 0 0 0 0 0 0 0 0 0 2 2 2
Average tic-toc performance over 1000 iterations 0.00018 seconds.
For more details about the employed functions, refer to the following links:
blkdiag
repmat
It is easy to do with kronecker product kron:
m = 3; % size of the blocks
n = 5; % number of blocks
A = rand(m); % insert you matrices here
D = rand(m);
maskA = zeros(n); % maskA is the block structure of A
maskA(1,:) = 1;
maskA(:,1) = 1;
maskA(1,1) = 0;
maskD = eye(n); %maskD is the block structure of D
B = kron(maskA,A) + kron(maskD,D);
Here is a way using cell2mat:
C = {zeros(size(A)), D , A};
mat = ones(n);
mat(1:n+1:end)=2;
mat(1,2:end)= 3;
mat(2:end,1)= 3;
out = cell2mat(C(mat));
Here's a way:
A = [10 20; 30 40]; % square matrix
D = [50 60; 70 80]; % square matrix
n = 5; % positive integer
tmp_A = repmat({A}, 1, n-1);
tmp_D = repmat({D}, 1, n-1);
result = [D, horzcat(tmp_A{:}); vertcat(tmp_A{:}), blkdiag(tmp_D{:})]

Matlab Find number of consecutive zeros

I want to find the lengths of series zeros in a matrix
A = [0 0 0 3 1 4 6 0 9 1 0 0 0 0 0 0 1 5 2 1 1;2 3 1 0 0 4 6 0 0 0 2 3 8 6 0 0 0 0 0 1 1]
I need result gives seriesZeros = [3 1 6;2 3 5] and also [rows,cols] from series of zeros value
thank you very much...
You could do this as follows:
A = [0 0 0 3 1 4 6 0 9 1 0 0 0 0 0 0 1 5 2 1 1;
2 3 1 0 0 4 6 0 0 0 2 3 8 6 0 0 0 0 0 1 1];
[N,~] = size(A);
% pad A==0 with zeros, and calculate diff for each row
A2 = diff([zeros(N,1) A==0 zeros(N,1)],[],2);
out_mtx = [];
for row_i = 1:size(A2,1)
row = A2(row_i, :);
zero_lengths = find(row == -1) - find(row == 1);
out_mtx(end+1,:) = zero_lengths;
end
out_mtx
Which gives
out_mtx =
3 1 6
2 3 5

Get all vector elements between different indices

I have one vector of starting points (start) and another vector for end points (end):
start=[1 0 0 0 1 0 0 0 1 0 0 0]
end= [0 0 1 0 0 0 1 0 0 0 1 0]
I want a third vector A all the numbers between each start and endpoint.
A = [1 2 3 4 5 6 7 8 9 1 2 3]
so the result for this example could look like this:
A_result= [1 2 3 5 6 7 9 1 2]
any ideas?
Without loop:
s=[1 0 0 0 1 0 0 0 1 0 0 0];
e=[0 0 1 0 0 0 1 0 0 0 1 0];
mask = cumsum(s)-cumsum([shift(e,1)])
# Will be [1 1 1 0 1 1 1 0 1 1 1 0]
A = [1 2 3 4 5 6 7 8 9 1 2 3];
A(find(mask))
# will be [1 2 3 5 6 7 9 1 2]
Or as The Minion pointed out, simply:
A(mask==1)
# will be [1 2 3 5 6 7 9 1 2]
Check the solution from seb it is way faster for large arrays.
This is a solution using a for-loop
d_start=[1 0 0 0 1 0 0 0 1 0 0 0];
d_end= [0 0 1 0 0 0 1 0 0 0 1 0];
A = [1 2 3 4 5 6 7 8 9 1 2 3];
starting_idx = find(d_start);
ending_idx = find(d_end);
A_result = [];
for l=1:numel(starting_idx)
A_result=horzcat(A_result, A(starting_idx(l):ending_idx(l)));
end
What I am doing is: 1. I get the index of the starting and end points (find). Then i predefine my result. Now i just have to loop over the number of entries in the starting points and add those values from that position to next ending to my result.
Edit: Timing test:
I used my solution as well as the solution from seb (modified to my variable names):
mask = cumsum(d_start)-cumsum([0 d_end(1:end-1)]);
B_res =A(find(mask));
as well as my comment to his solution:
mask = cumsum(d_start)-cumsum([0 d_end(1:end-1)]);
B_res =A(mask==1);
These were the timing results for A:[12888x1]double
t_forloop : 33 sec
t_seb_find: 0.31 sec
t_seb_logical: 0.1964 sec

How to get the unique slices of a matrix?

In matlab, if you have a matrix A you can find the matrix B containing all of the unique rows of A as follows:
B = unique(A,'rows');
What I have is a 3d matrix, with rows and columns as the first two dimensions, and one additional dimension ('slices').
How can I get the 3d matrix containing all the unique slices in a matrix A? Here's an example of the kind of functionality I want:
>> A % print out A
A(:,:,1) =
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
A(:,:,2) =
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
A(:,:,3) =
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
A(:,:,4) =
0 0 0 1
0 0 1 0
0 1 0 0
1 0 0 0
>> unique(A,'slices'); % get unique slices
A(:,:,1) =
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
A(:,:,2) =
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
A(:,:,3) =
0 0 0 1
0 0 1 0
0 1 0 0
1 0 0 0
I would begin by reshaping A so each slice becomes a row (with the reshape command). Then use unique(A, 'rows'). Finally, reshape the unique rows back to the same shape the slices.
For example:
% transforming so each row is a slice in row form
reshaped_A = reshape(A, [], size(A, 3))';
% getting unique rows
unique_rows = unique(reshaped_A, 'rows');
% reshaping back
unique_slices = reshape(unique_rows', size(A, 1), size(A, 2), []);
Or all in one line:
reshape(unique(reshape(A, [], size(A, 3))', 'rows')', size(A, 1), size(A, 2), [])
I haven't checked this above code so use with caution! But it should give the idea.
EDIT
Here it is working on your data (also fixed little bug in above code):
>> reshaped_A = reshape(A, [], size(A, 3))'
reshaped_A =
Columns 1 through 11
16 5 9 4 2 11 7 14 3 10 6
1 0 0 0 0 1 0 0 0 0 1
16 5 9 4 2 11 7 14 3 10 6
0 0 0 1 0 0 1 0 0 1 0
Columns 12 through 16
15 13 8 12 1
0 0 0 0 1
15 13 8 12 1
0 1 0 0 0
Each of these ^^ rows is one of the original slices
>> unique_rows = unique(reshaped_A, 'rows')
unique_rows =
Columns 1 through 11
0 0 0 1 0 0 1 0 0 1 0
1 0 0 0 0 1 0 0 0 0 1
16 5 9 4 2 11 7 14 3 10 6
Columns 12 through 16
0 1 0 0 0
0 0 0 0 1
15 13 8 12 1
These ^^ are the unique slices, but in the wrong shape.
>> unique_slices = reshape(unique_rows', size(A, 1), size(A, 2), [])
unique_slices(:,:,1) =
0 0 0 1
0 0 1 0
0 1 0 0
1 0 0 0
unique_slices(:,:,2) =
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
unique_slices(:,:,3) =
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
A very simple and scalable solution would be:
A = cat(3, [16 2 3 13;5 11 10 8;9 7 6 12;4 14 15 1], [1 0 0 0;0 1 0 0;0 0 1 0;0 0 0 1], [16 2 3 13;5 11 10 8;9 7 6 12;4 14 15 1], [0 0 0 1;0 0 1 0;0 1 0 0;1 0 0 0])
[n,m,p] = size(A);
a = reshape(A,n,[],1);
b = reshape(a(:),n*m,[])';
c = unique(b,'rows', 'stable')'; %If the 'stable' option is supported by your version.
%If the 'stable' option is not supported, but it's still required, use the index vector option, as required.
%i.e.,
%[c,I,J] = unique(b,'rows');
unique_A = reshape(c,n,m,[])
Results:
A(:,:,1) =
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
A(:,:,2) =
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
A(:,:,3) =
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
A(:,:,4) =
0 0 0 1
0 0 1 0
0 1 0 0
1 0 0 0
unique_A(:,:,1) =
0 0 0 1
0 0 1 0
0 1 0 0
1 0 0 0
unique_A(:,:,2) =
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
unique_A(:,:,3) =
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
Source: How to find unique pages in a 3d matrix?

Matrix in matlab?

I am working on Connected Components labeling, and my matrix is:
1 1 0 2 2 2 0 3
1 1 0 2 0 2 0 3
1 1 1 1 0 0 0 3
0 0 0 0 0 0 0 3
4 4 4 4 0 5 0 3
0 0 0 4 0 5 0 3
6 6 0 4 0 0 0 3
6 6 0 4 0 7 7 7
and now, i want to do the second scan over it, for this i have made following code:
for i=1:1:r
for j=1:1:c
if (bw(i,j)>=1)
if (i-1>0 & i+1<=r)
% if ( bw(i,j)~= bw(i-1,j) | bw(i,j)~= bw(i+1,j))
if ( (bw(i,j)~= bw(i-1,j) & bw(i-1,j)>0))
bw(i,j)= min (bw(i-1,j),bw(i,j))
elseif ((bw(i,j)~= bw(i+1,j) & bw(i+1,j)>0))
bw(i,j) = min(bw(i+1,j),bw(i,j));
end
end
if (j-1>0 & j+1<=c)
if ( (bw(i,j)~= bw(i,j-1) & bw(i,j-1)>0))
bw(i,j) = min (bw(i,j-1),bw(i,j));
elseif((bw(i,j)~= bw(i,j+1) & bw(i,j+1)>0))
bw(i,j) = min (bw(i,j+1),bw(i,j)) ;
end
end
end
end
end
disp(bw);
but the problem is, when i run this code, i get the following output
1 1 0 2 2 2 0 3
1 1 0 1 0 2 0 3
1 1 1 1 0 0 0 3
0 0 0 0 0 0 0 3
4 4 4 4 0 5 0 3
0 0 0 4 0 5 0 3
6 6 0 4 0 0 0 3
6 6 0 4 0 7 7 7
only one value changes (2nd row, 4 col) in my result, whereas, I want:
1 1 0 1 1 1 0 3
1 1 0 1 0 1 0 3
1 1 1 1 0 0 0 3
0 0 0 0 0 0 0 3
4 4 4 4 0 5 0 3
0 0 0 4 0 5 0 3
6 6 0 4 0 0 0 3
6 6 0 4 0 3 3 3
could somebody please help? where am i making a mistake?
Nice solution by Jonas. As I was a bit into coding when I saw it added, I thought I'd show you my solution as well. Hopefully it is a bit more similar to your original code.
One of the mistakes you made was assuming this connection could be done in just one pass. Generaly it can not, as detections of some of the "snake elements" are dependent on the way you iterate throught the matrix. Becuase of this, I added an outer while loop.
bw =[ 1 1 0 2 2 2 0 3;
1 1 0 2 0 2 0 3;
1 1 1 1 0 0 0 3;
0 0 0 0 0 0 0 3;
4 4 4 4 0 5 0 3;
0 0 0 4 0 5 0 3;
6 6 0 4 0 0 0 3;
6 6 0 4 0 7 7 7 ];
%Set up matrix
[r,c] = size(bw);
%Zero pad border
bwZ = [zeros(1,c+2);[zeros(r,1) bw zeros(r,1)];zeros(1,c+2)];
%Iterate over all elements within zero padded border
done=0;%Done flag
cc=1; %Infinite loop protection
while not(done) && cc<r*c
done=1;cc=cc+1;
for i=2:r+1
for j=2:c+1
%Point should be evaluated
p = bwZ(i,j);
if p >= 1
%Pick out elements around active elements
if bwZ(i-1,j)==0;ue=inf;else ue = bwZ(i-1,j); end;%Up element
if bwZ(i+1,j)==0;de=inf;else de = bwZ(i+1,j); end;%Down element
if bwZ(i,j-1)==0;le=inf;else le = bwZ(i,j-1); end;%Left element
if bwZ(i,j+1)==0;re=inf;else re = bwZ(i,j+1); end;%Right element
bwZ(i,j) = min([ue de le re]);
%Set flag, if something has changed
if bwZ(i,j) ~= p
done = 0;
end
end
end
end
end
%Remove zero padding
bw = bwZ(2:end-1,2:end-1)
Output:
bw =
1 1 0 1 1 1 0 3
1 1 0 1 0 1 0 3
1 1 1 1 0 0 0 3
0 0 0 0 0 0 0 3
4 4 4 4 0 5 0 3
0 0 0 4 0 5 0 3
6 6 0 4 0 0 0 3
6 6 0 4 0 3 3 3
If the numbers don't need to be preserved, you can simply call bwlabel on your original image:
newImage = bwlabel(originalImage>0);
EDIT
Here's another version. It checks each connected component to see whether there's another connected component touching it. If yes, that connected component is relabelled.
%# nCC: number of connected components
nCC = max(originalImage(:));
for cc = 1:nCC
%# check whether the component exists
myCC = originalImage==cc;
if any(any(myCC))
%# create a mask to check for neighbors
%# by creating a border of 1 pixel
%# around the original label
msk = imdilate(myCC,true(3)) & ~myCC;
%# read all the pixel values under the mask
neighbours = originalImage(msk);
%# we're not interested in zeros, remove them
neighbours = neighbours(neighbours > 0);
if ~isempty(neighbours)
%# set the label of all neighbours to cc
originalImage( ismember(originalImage,neighbours) ) = cc;
end
end
end