Reputation: 3616
I have a A
which is 640x1 cell. where the value of each cell A(i,1)
varies from row to row, for example A(1,1) =[]
, while A(2,1)=[1]
and A(3,1)=[1,2,3]
.
There is another matrix B
of size 480x640, where the row_index (i)
of vector A
corresponds to the col_index of matrix B
. While the cell value of each row in vector A
corresponds to the row_index in matrix B
. For example, A(2,1)=[1] this means col_2 row_1 in matrix B
, while A(3,1)=[1,2,3] means col_3 rows 1,2&3 in matrix B
.
What I'm trying to do is to for each non-zero value in matrix B
that are referenced from vector A
, I want to check whether there are at least 4 other neighbors that are also referenced from vector A
. The number neighbors of each value are determined by a value N
.
For example, this is a part of matrix B
where all the zeros"just to clarify, as in fact they may be non-zeros" are the neighbors of pixel X
when N=3
:
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 X 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
As shown, because N=3
, all these zeros are pixel X
's neighbors. So if more than 4 neighbor pixels are found in vector A
then do something e.g G=1
if not then G=0
;
So if anyone could please advise. And please let me know if any more clarification is needed.
Upvotes: 1
Views: 1580
Reputation: 7751
Since you have a one-to-one correspondence between A
and B
, there is no need to work on A
. B is a logical matrix (0 if not referenced in A
, 1 if referenced). You can therefore apply a simple filter2
function counting the number of active neighbors within the 8 closest elements.
Here is the code
B = rand(10,10); %generate binary matrix
h = [1 1 1;1 0 1;1 1 1]; %filter to be applied
filter2(h,B,'same')>=4 & B>0 %apply filter on B, count minimum of 4 neighbors, if only B>1
EDIT
To transform a cell array B
into binary presence (0=empty, 1=not empty), use of cellfun
is straightforward
B = ~cellfun(@isempty,B);
And see Armo's response to your previous question for how to create B based on A.
Upvotes: 1
Reputation: 3137
The first thing I would do is to convert your cell of indices A
to a logic matrix Amat
. This makes it easier to check how many neighbours are included in A
.
Here is a solution that uses this conversion. I hope the comments are enough to make it understandable.
clear all
clc
nCols = 7;
nRows = 6;
N = 3; %// Number of neighbours
M = 4; %// Minimum number of wanted connections
%// Create cell of indices A
A = cell(nCols,1);
A{1} = [];
A{2} = 1;
A{3} = [1 2 3];
A{4} = [2 5];
A{5} = 3;
A{6} = [3 5];
A{7} = [1 4 6];
%// Generate radom data B
%// (There is a 50% probability for each element of B to be zero)
Bmax = 17;
B = (randi(2,nRows,nCols)-1).*(randi(Bmax,nRows,nCols));
%// Convert the cell A to a logic matrix Amat
Amat = zeros(size(B));
for ii = 1:nCols
Amat(A{ii},ii) = 1;
end
A
B
Amat
for ii = 1:nCols
for jj = A{ii}
if B(jj,ii)>0
%// Calculate neighbour indices with a lower bound of 1
%// and an upper bound of nCols or nRows
col_lim_low = max(1,ii-N);
col_lim_high = min(nCols,ii+N);
row_lim_low = max(1,jj-N);
row_lim_high = min(nRows,jj+N);
%// Get the corresponding neighbouring-matrix from Amat
A_neighbours = ...
Amat(row_lim_low:row_lim_high,col_lim_low:col_lim_high);
%// Check the number of neighbours against the wanted number M
if sum(A_neighbours(:)) > 1 + M
%# do something
fprintf('We should do something here at (%d,%d)\n',jj,ii)
end
end
end
end
The following is a printout from one run of the code.
A =
[]
[ 1]
[1x3 double]
[1x2 double]
[ 3]
[1x2 double]
[1x3 double]
B =
1 5 0 0 11 0 16
0 13 13 0 0 0 9
0 0 0 5 0 0 0
3 8 16 16 0 2 12
0 0 5 0 9 9 0
12 13 0 6 0 15 0
Amat =
0 1 1 0 0 0 1
0 0 1 1 0 0 0
0 0 1 0 1 1 0
0 0 0 0 0 0 1
0 0 0 1 0 1 0
0 0 0 0 0 0 1
We should do something here at (1,2)
We should do something here at (2,3)
We should do something here at (5,6)
We should do something here at (4,7)
Upvotes: 2