Extracting neighbouring cells in a cell array
13 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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.'})
Now I have the matrix C with the cells of interest
C=[1 2; 1 2]
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]
Nbh2=[1 5 3 2]
I have 3 problems:
- (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.
- 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
- 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
il 1 Lug 2024
Modificato: Stephen23
il 1 Lug 2024
"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.
Risposta accettata
Voss
il 1 Lug 2024
B = [1,2,2,1,1; 2,1,2,1,2];
V = 1:size(B,2);
A = accumarray(B.',V(:),[],@(m){m.'})
C=[1 2; 1 2]
% 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{:}
% 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
3 Commenti
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Matrix Indexing in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!