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

  • SEARCH
  • Home
  • 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 8345893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T06:49:53+00:00 2026-06-09T06:49:53+00:00

I have a M x N sized structure array with fields var and val

  • 0

I have a M x N sized structure array with fields var and val which are vectors.

What I would like to do is to get an M x N sized matrix A where each element A(i, j) contains the sum value of vector var (or val) from structure array

For example:

myStructure(1,5)

ans = 

    var: 1
    val: [0.0100 0.1800 0.8100]

sum(myStructure(1,5).val)

ans = 

    1

myStructure(7,8)

ans = 

     var: [1 3]
     val: [1x9 double]

myStructure(7,8).val

ans =

  Columns 1 through 6

    0.1111    0.1111    0.1111    0.1111    0.1111    0.1111

  Columns 7 through 9

    0.1111    0.1111    0.1111

Therefore A(1,5) should be 1 and the same way all elements A(i,j) should be equal to sum(myStructure(i,j).val).

Does anyone know how this could be done in Matlab without using for loops?

I’ve tried to use sum function in a number of ways (sum(messages.val) and sum(messages(:,:).val) …) but couldn’t get the desired result.

  • 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-09T06:49:55+00:00Added an answer on June 9, 2026 at 6:49 am

    You can get the field elements into one matrix using:

    svals = [myStructure.val];
    

    If val is always the same length (let’s name it P), this’ll be a numel(myStructure)*P x 1 vector containing all values of all fields in sequence. You can reshape it of course back:

    [N,M]=size(myStructure);
    P = numel(myStructure(1,1).val);
    svals = reshape(svals,[P M N]);
    

    and now just sum the first dimension, which leaves you the MxN sized A matrix:

    A = squeeze(sum(svals,1));
    

    squeeze is applied in this last step to remove the resulting singleton dimension (otherwise A would be of size 1xMxN).

    If the vallength can vary, I see no other way than looping it, or using arrayfun, which is essentially the same as looping:

    A = arrayfun(@(x) sum(x.val),myStructure);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a PHP multidimensional associative array with the following structure: Fields: TYPE -
I have a structure like so: Basis.FieldsBasisType.fieldsBasisComponents There are ~13 components to each basis,
I have a moderately sized dataset in excel from which I wish to extract
So i have this XML structure: <fields> <field name=agent_description label=Agent Description size=area /> <field
I have hundreds of thousands of NumPy boolean arrays that I would like to
I have a plist which is an array, the file name is called startups.plist.
I have a script which gets some values from a DB. The structure of
I have structure with 10 fields of the same size. The structure was read
Say I have a dynamic array like: int* integers = new int[100]; Is there
I have a class like this: //Array of Structures class Unit { public: float

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.