Extracting neighbouring cells in a cell array
11 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
Stephen23
2024 年 7 月 1 日
編集済み: Stephen23
2024 年 7 月 1 日
"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.
採用された回答
Voss
2024 年 7 月 1 日
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 件のコメント
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Matrix Indexing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!