从单元阵列中的每个单元中提取特定元素 [英] Extracting a specific element from each cell within cell array

查看:71
本文介绍了从单元阵列中的每个单元中提取特定元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个大小为10x10的单元格数组A(例如).每个单元格依次包含一个5x20矩阵.我想从每个单元格中选择(i,j)元素,其中(i,j)是循环内的索引.我可以运行4个for循环并轻松获得答案. 可能甚至更快,因为已经多次讨论了循环可能比cellfun,structfun等更快.

I have a cell array A of size 10x10 (say). Each cell in turn contains a 5x20 matrix. I want to select (i,j) element from each cell, where (i,j) are indices within a loop. I can run 4 for loops and easily get the answer. It may even be faster as it has been discussed many times that loops could be faster than cellfun, structfun etc.

仍然,有没有使用cellfun的解决方案,可以在(i,j)上循环使用,并提取每个单元格中的(i,j)元素?我尝试编写一个函数来充当cellfun的句柄,但无法向下访问两个级别,即A{eachCellRow,eachCellCol}(i,j).

Still, is there any solution using cellfun which I can use in a loop over (i,j) and extract (i,j) element in each cell? I tried writing a function which will act as handle to cellfun but I couldn't access two-leves down i.e. A{eachCellRow,eachCellCol}(i,j).

示例: 如果A={[1 2;5 6], [3 4; 6 7]; [3 4; 6 7], [9 8; 5 6]};

然后i=1, j=1 and i=2, j=1输出应为:

B=[1 3; 3 9] and B=[5 6; 6 5]

推荐答案

CELL2MAT 将所有仅包含数字数据的单元格数组中的数据获取到数字数组中.因此,这对我们有帮助.对于您的原始问题,请尝试以下操作-

CELL2MAT gets all the data from a cell array that consists of numeric data only, into a numeric array. So, that helped us here. For your original problem, try this -

celldim_i = 10;
celldim_j = 10;

block_size_i = 5;
block_size_j = 20;

search_i = i; %// Edit to your i
search_j = j; %// Edit to your j

A_mat = cell2mat(A);
out = A_mat(search_i+block_size_i*(0:celldim_i-1),search_j+block_size_j*(0:celldim_j-1))

这篇关于从单元阵列中的每个单元中提取特定元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆