Cellfun与Structfun函数? [英] Cellfun with Structfun as function?

查看:219
本文介绍了Cellfun与Structfun函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据是以结构单元阵列的形式出现的。我正在尝试实现一个以structfun为函数的cellfun调用,在一天结束时,它将调整结构中的所有向量的大小以传入大小。例如。我有一个4单元的数组,每个数组都有一个包含一个矢量的结构(一旦我找到了这个结构,它就成为多个矢量),我想调整每个矢量从索引1到索引2的大小。

  fun =函数(foo,index1,index2)
cellfun(@structfun(@(x)x(index1:index2),foo,'UniformOutput',false ),foo,'UniformOutput',false)

我必须做一个循环并且替换第一个foo与foo(i)能够到达所有单元格?提前致谢。

解决方案

不能像这样内联structfun。你需要创建一个调用structfun并在cellfun中使用它的临时函数句柄。



$ p $ s $ =(y)structfun(@(x)x(1:2),y,'UniformOutput',false );
cellfun(sf,foo,'UniformOutput',false);

您可以在一行中做到这一点,如下所示。但为了便于阅读,最好将它分成两行。

  cellfun(@(y)structfun(@(x)x (1:2),y,'UniformOutput',false),foo,'UniformOutput',false); 


My data are in the form of a cell array of structs. I am trying to implement a cellfun call that takes structfun as its function that, at the end of the day, will resize all of the vectors in the struct to the passed-in size. E.g. I have a 4-cell array each with a struct that contains one vector (going to be multiple vectors once I figure this out), and I want to resize each vector from index1 to index2

fun = function(foo, index1, index2) 
cellfun(@structfun(@(x) x(index1:index2), foo, 'UniformOutput',false), foo, 'UniformOutput', false)

Do I have to do a loop and replace the first "foo" with "foo(i)" to be able to reach all the cells? Thanks in advance.

解决方案

You cannot inline structfun like that. You need to create a temporary function handle which calls structfun and use that in cellfun.

sf = @(y) structfun(@(x) x(1:2), y, 'UniformOutput',false); 
cellfun(sf, foo, 'UniformOutput', false);

You can do this in one line as below. But it is better to keep this in two lines for readability.

cellfun(@(y) structfun(@(x) x(1:2), y, 'UniformOutput',false), foo, 'UniformOutput', false);

这篇关于Cellfun与Structfun函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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