Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8702609
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:41:43+00:00 2026-06-13T02:41:43+00:00

I’ve got an struct array, with three fields – an array, the array’s length,

  • 0

I’ve got an struct array, with three fields – an array, the array’s length, and a number.

N = 5;
data = struct;
for i=1:N
    n = ceil(rand * 3);
    data(i).len = n;
    data(i).array = rand(1,n);
    data(i).number = i;
end

The data looks like this:

data = 
1x5 struct array with fields:
    len    = [ 1 3 3 1 1 ]
    array  = [[0.8]; [0.7 0.9 0.4]; [0.7 0 0.3]; [0.1]; [0.3]]
    number = [ 1 2 3 4 5 ]

I can return array as a 1×9 array in several ways:

>>> [data.array] 
>>> cat(2,data.array)
[0.8 | 0.7 0.9 0.4 | 0.7 0 0.3 | 0.1 | 0.3]     %  | shows array separation

I’d like to repeat the number (data.number) len times, to produce the same length array as the concatenated array.

I’m currently doing this with arrayfun then cell2mat:

>> x = arrayfun(@(x) repmat(x.number, 1, x.len), data, 'UniformOutput', false)
x = 
    [1]    [1x3 double]    [1x3 double]    [4]    [5]
>> cell2mat(x)
[ 1 2 2 2 3 3 3 4 5]

This makes the numbers line up with the arrays.

arrays =  [ 0.8 | 0.7 0.9 0.4 | 0.7 0 0.3 | 0.1 | 0.3 ] 
numbers = [ 1   | 2   2   2   | 3   3   3 | 4   | 5   ]

The idea behind this is to feed the data to the GPU for processing – but rearranging the data takes orders of magnitude longer than the actual processing.

Arrayfun takes ~5 seconds when N=100,000, and a for loop calling repmat takes ~4 seconds.

Is there a faster way to rearrange data from uneven arrays in structures into matching length 1d arrays? I’m open to using a different data structure.


Testing vectorised method:

data = struct;
data(1).len = 1;
data(1).array = [1 2 3];
data(1).number = 11;
data(2).len = 0;
data(2).array = [];
data(2).number = 12;
data(3).len = 2;
data(3).array = [4 5 6; 7 8 9];
data(3).number = 13;

list_of_array = cat(1,data.array)

idx = zeros(1,size(list_of_array,1));
% Set start of each array to 1
len = cumsum([data.len])
idx(len) = 1
% Flat indices
idx = cumsum([1 idx(1:end-1)])

nf = [data.number]
repeated_num_faces = nf(idx)

Gives the output:

list_of_array =
     1     2     3
     4     5     6
     7     8     9
len =
     1     1     3    % Cumulative lengths
idx =
     1     0     1    % Ones at start
idx =
     1     2     2    % Flat indexes - should be [1 3 3]
nf =
    11    12    13    % Numbers expanded
repeated_num_faces =
    11    12    12    % Wrong .numbers - should be [11 13 13]
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T02:41:46+00:00Added an answer on June 13, 2026 at 2:41 am

    Well, struct is not the easiest to deal with here. Definitely, you should not use repmat. Rather than that, preallocate the data_number array and do a for loop:

    tic;
    data_array  = [data(:).array];
    data_number = zeros(size(data_array));
    start = 1;
    for i=1:N
        nel = data(i).len;
        data_number(start:start+nel-1) = data(i).number;
        start = start+nel;
    end
    toc;
    

    Here is another ‘vectorized’ solution using cumsum to mark the indices in the ‘flat’ vector

    tic;
    data_array  = [data.array];
    data_number = zeros(size(data_array));
    
    % cumulative sum of number of elements in every array
    len = cumsum([data.len]);
    
    % mark the end of every array in a 'flat' vector
    data_number(len) = 1;
    
    % compute 'flat' indices for every data(i).array
    data_number = cumsum([1 data_number(1:end-1)]);
    
    % extract the data.number field
    data_num = [data.number];
    data_number = data_num(data_number);
    toc;
    

    For a data set of N=1e5 the times are:

    Elapsed time is 0.153539 seconds.
    Elapsed time is 0.110694 seconds.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I've got a string that has curly quotes in it. I'd like to replace
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I would like to count the length of a string with PHP. The string
I have an autohotkey script which looks up a word in a bilingual dictionary
I have an array which has BIG numbers and small numbers in it. I
i got an object with contents of html markup in it, for example: string
I want to construct a data frame in an Rcpp function, but when I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.