从结构的多个字段中提取行

M.P*_*til 3 indexing matlab struct reference cell-array

创建列向量(100行)的结构(25个字段).如何提取其特定行.例如,

s.a=[1 2 3 4 5 6]'
s.b=[5 2 8 1 0 4]'
s.c=[9 7 0 1 3 5]'
% 2 to 4 rows to be extracted
% expected output
t.a=[2 3 4]'
t.b=[2 8 1]'
t.c=[7 0 1]'
Run Code Online (Sandbox Code Playgroud)

对struct的索引不起作用.什么是通用的方式.

obc*_*don 7

你可以简单地使用 structfun

t = structfun(@(x)x(2:4),s,'UniformOutput',false)
Run Code Online (Sandbox Code Playgroud)

  • `structfun`,`arrayfun`和所有这类函数只是隐藏了一个for循环,所以它不是更快,但它更容易阅读.你可以将几个有趣的函数组合在一起,例如,如果你有一个可以编写的结构数组:`arrayfun(@(y)structfun(@(x)x(2:4),y,'UniformOutput',false) ,s)`而不是2嵌套for循环. (4认同)