Azzera filtri
Azzera filtri

Extracting neighbouring cells in a cell array

36 visualizzazioni (ultimi 30 giorni)
OK
OK il 1 Lug 2024 alle 10:07
Commentato: Voss il 1 Lug 2024 alle 19:44
This is a follow up to this question. I have cell array and a list of cells of interests (given as a matrix, whose columns correspond to the indices of cells). I want to access and concatenate the entries of all cells located +/- 1 the cell of interest.
For example, as suggested in the accepted answer to the references question, I construct the cell array A as follows
B = [1,2,2,1,1; 2,1,2,1,2];
V = 1:size(B,2);
A = accumarray(B.',V(:),[],@(m){m.'})
A = 2x2 cell array
{[4]} {[1 5]} {[2]} {[ 3]}
Now I have the matrix C with the cells of interest
C=[1 2; 1 2]
C = 2x2
1 2 1 2
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
I have 2 neighbourhoods that I want to explore
NbhInd1=[1 1 2; 1 2 1];
NbhInd2=[1 2 2; 2 2 1];
In the end, I want to be able to get two arrays of neighbourhoods (can be sorted or unsorted)
Nbh1=[4 1 5 2]
Nbh1 = 1x4
4 1 5 2
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Nbh2=[1 5 3 2]
Nbh2 = 1x4
1 5 3 2
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
I have 3 problems:
  1. (Same as in the referenced question): I don't know how to convert an array into a proper index to refer to the correct cell of A. I.e. A{[1,1]} or A ([1,1]) is not the same as A{1,1}, and I need to the latter.
  2. How to automate the construction of indices of the neighbourhoods. In principle, I can use combinations() but it gives too many indices. Also, I'm not sure how to automatically easily convert [1,1] into table2array(combinations(1:2,1:2)), i.e. splitting an array into its coordinates and manipulating separately
  3. The true array A has high dimensionality (e.g. size(A)=repmat(9,[1,10])), so I'd like to minimize the number of loops.
  3 Commenti
Stephen23
Stephen23 il 1 Lug 2024 alle 18:01
Modificato: Stephen23 il 1 Lug 2024 alle 18:18
"I want to access and concatenate the entries of all cells located +/- 1 the cell of interest."
How many times do you need to perform this operation? With how many indices? Your data arrays are large, so if you want an efficient approach you might need to think outside the square, perhaps based on interpolation or convolution or something of that ilk. Another option might be to use some image processing tools.
OK
OK il 1 Lug 2024 alle 19:38
@Stephen23 good question, thanks for raising this. The story is that I have a large set S of points (in principle, can be arbitrarily large, for now 10^6) and I have another set S' of points (~|S'|=1000) for which I want to study a certain property P, i.e. for each x in S', I want to know whether P(x)==true.
To compute P(x), I need to understand how x is positioned with respect to the its neighbours in S (e.g. all points in S that lie within some distance d from x). Which means that I need to select the neighbourhood of x in S. For now I am extracting the neighbourhoods using Euclidean distance, but this is pretty slow.
My hope is that by first putting S on a grid and then restricting the search space to the neighbouring cells instead of the entire S should speed up the process.

Accedi per commentare.

Risposta accettata

Voss
Voss il 1 Lug 2024 alle 17:53
B = [1,2,2,1,1; 2,1,2,1,2];
V = 1:size(B,2);
A = accumarray(B.',V(:),[],@(m){m.'})
A = 2x2 cell array
{[4]} {[1 5]} {[2]} {[ 3]}
C=[1 2; 1 2]
C = 2x2
1 2 1 2
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
% construct NbhInd (a cell array of neighborhood index matrices) from C
N = size(C,2);
NbhInd = cell(1,N);
sizA = size(A);
NsA = numel(sizA);
assert(size(C,1) == NsA)
offsets = [zeros(1,NsA); eye(NsA); -eye(NsA)];
for ii = 1:N
NbhInd{ii} = unique(min(sizA,max(1,C(:,ii).'+offsets)),'rows').';
end
NbhInd{:}
ans = 2x3
1 1 2 1 2 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
ans = 2x3
1 2 2 2 1 2
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
% take elements from A within each neighborhood
Nbh = cell(1,N);
for ii = 1:N
M = size(NbhInd{ii},2);
idx = num2cell(NbhInd{ii});
temp = cell(1,M);
for jj = 1:M
temp{jj} = A{idx{:,jj}};
end
Nbh{ii} = [temp{:}];
end
Nbh
Nbh = 1x2 cell array
{[4 1 5 2]} {[1 5 2 3]}
  3 Commenti
Voss
Voss il 1 Lug 2024 alle 19:44
You're welcome!

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Matrices and Arrays in Help Center e File Exchange

Prodotti


Release

R2023b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by