File: soa2aos.m

package info (click to toggle)
octave-iso2mesh 1.9.8%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 11,128 kB
  • sloc: cpp: 11,982; ansic: 10,158; sh: 365; makefile: 59
file content (41 lines) | stat: -rw-r--r-- 1,079 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
function as = soa2aos(starray)
%
%    as=soa2aos(starray)
%
%    Convert a struct-of-arrays (SoA) to an array-of-structs (AoS)
%
%    author: Qianqian Fang (q.fang <at> neu.edu)
%
%    input:
%        starray: a struct array, with each subfield of numeric vectors
%
%    output:
%        as: a struct array, containing the same number of subfields as starray
%             with each subfield a single scalar; the length of
%
%    example:
%        a=struct('a',[1,2],'b',[3,4]);
%        st=soa2aos(a)
%
%    this file is part of JSNIRF specification: https://github.com/NeuroJSON/jsnirf
%
%    License: GPLv3 or Apache 2.0, see https://github.com/NeuroJSON/jsnirf for details
%

if (nargin < 1 || ~isstruct(starray))
    error('you must give a struct with subfield of numeric vectors');
end

if (length(starray) > 1)
    as = starray;
    return
end

allsize = structfun(@numel, starray);
if (length(unique(allsize)) > 1)
    as = starray;
    return
end

strcell = [fieldnames(starray) struct2cell(structfun(@num2cell, starray, 'uniformoutput', 0))]';
as = struct(strcell{:});