MATLAB:确定一个结构数组的总长度/大小的结构阵列领域 [英] MATLAB: Determine total length/size of a structure array with fields as structure arrays

查看:725
本文介绍了MATLAB:确定一个结构数组的总长度/大小的结构阵列领域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含字段长度可变的结构数组结构数组。例如:

I have a structure array containing fields as structure arrays of varying length. For example:

S是一个结构
数据是在s的一个字段,并且也是结构阵列本身

's' is a structure 'data' is a field in 's', and also a structure array itself and

length(s(n).data) ~= length(s(m).data)

我要preallocate一个数组,需要一个时间标记,从各个领域s.data.timestamp。
有没有办法做到这一点不使用循环两次吗?这是我到目前为止有:

I want to preallocate an array that takes a time stamp from every field s.data.timestamp. Is there a way to do this without using a for loop twice? This is what I have so far:

% find the total length
count=0;
for x=1:length(s)
  count=count+length(s(x).data);
end
% preallocate timestamp array
timestamp=zeros(1,count);
% populate timestamp array
index=1;
for x=1:length(s)
   for y=1:length(s(x).data)
      timestamp(index)=s(x).data(y).timestamp;
      index=index+1;
   end
end

我想过只是高估了,我需要根据S和数据的平均长度的长度长,但每个'数据'字段/子结构的实际长度相差很大。我会关闭只是高估的挫折感了出来,并修剪后得到的数组更好吗?零时间戳是不可能的数据集我的工作,所以这不应该是一个问题。

I thought about just overestimating the length that I would need based on the length of 's' and an average length of 'data', but the actual length of each 'data' field/substructure varies widely. Would I be better off just overestimating the heck out of it and trimming the resulting array afterward? Zero timestamps are impossible with the data set I'm working with, so that shouldn't be a problem.

推荐答案

这将工作,如果每一个结构数组数据具有相同的字段,并行向量(即<强> 1-由-N )

This will work if every structure array data has the same fields and are row vectors (i.e. 1-by-N):

allData = [s.data];               %# Concatenate all data arrays into one
timestamp = [allData.timestamp];  %# Collect all the time stamps

如果在数据结构数组是列向量(即 n乘1 ),你需要使用的 VERTCAT 来代替:

If the data structure arrays are column vectors (i.e. N-by-1), you need to use VERTCAT instead:

allData = vertcat(s.data);        %# Concatenate all data arrays into one
timestamp = [allData.timestamp];  %# Collect all the time stamps

上面的解决方案的工作由于在访问一个结构阵列的单个字段返回逗号分隔的列表

这篇关于MATLAB:确定一个结构数组的总长度/大小的结构阵列领域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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