从八度结构中提取给定字段名的所有行的值 [英] Extract values for all row, for given fieldname, from an Octave struct

查看:59
本文介绍了从八度结构中提取给定字段名的所有行的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从八度 struct 的所有行中获取完整列(字段名)的所有值?

How can I get all values of a complete column (fieldname), for all rows, from an Octave struct?

我最好将其放入单元格数组或常规向量中,最好不要循环.

I would get it into a cell array, or a regular vector, preferably without looping.

推荐答案

您似乎有些困惑.部分原因是您将结构与"R dataframes/python pandas"进行等价比较.

You seem to be confusing a few things. Partly because of your equivalence comparison of structs to "R dataframes / python pandas".

最好将结构与python字典,R列表等类似.它们是一个特殊的对象,可以容纳字段",可以通过字段名"(或键访问的值,如果您访问)更喜欢).

Structs are better thought of as being similar to python dicts, R lists, etc. They are a special object that can hold 'fields', which can be accessed by a 'fieldname' ( or values accessed by keys, if you prefer ).

同样,与八度中的任何其他对象一样,它们也是数组的有效元素.这意味着您可以拥有这样的东西:

Also, like any other object in octave, they are valid elements for an array. This means you can have something like this:

octave:1> struct( 'name', { 'Tom', 'Jim'; 'Ann', 'Sue' }, 'age', { 20, 21; 22, 23 } )
S =
  2x2 struct array containing the fields:
    name
    age

通常,当一个人处理这样的结构 array 时,访问数组中多个元素上的字段会产生

In general, when one deals with such a struct array, accessing a field on more than one elements of the array, produces a comma separated list. E.g.

octave:6> S(2,:).name
ans = Ann
ans = Sue

此函数可以传递给(例如扩展为")任何期望以逗号分隔的列表作为参数的函数.例如

This can be passed to (i.e. "expanded into") any function that expects such a comma separated list as arguments. E.g.

octave:7> fprintf( 'The girls names are %s, and %s.\n', S(2,:).name )
The girls names are Ann, and Sue.

如果需要,还可以将该列表直接传递到单元构造器"中,以创建单元.(尽管如果您希望它具有特定的形状,则必须在后缀后重塑它的形状).例如

If you want, you can also pass that list straight into a 'cell constructor', to create a cell. (though if you want it to have a particular shape, you'll have to reshape it afterwords). E.g.

octave:9> reshape( { S.age }, size(S) )
ans =
{
  [1,1] =  20
  [2,1] =  22
  [1,2] =  21
  [2,2] =  23
}

也有 struct2cell ,但这有所不同.尝试一下以查看其作用(例如 C = struct2cell(S)).

There is also struct2cell but this does something different. Try it to see what it does (e.g. C = struct2cell(S) ).

最后,为避免混淆,考虑到当处理结构数组时,列"是指数组"中的列,因此,我将避免使用该术语指代字段名".

Finally, to avoid confusion, given the fact that when one deals with struct arrays, "columns" refer to columns in the 'array', I would avoid referring to "fieldnames" by that term.

这篇关于从八度结构中提取给定字段名的所有行的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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