2

I have a quick question to ask. I am tryig to create a function called 'passavg_structure' that takes as an input argument an airspace structure array (which will be shown below), and outputs the average amount of passengers heading to the specified city.

The airspace structure array looks like this:

>> airspace(1)

ans = 

     Flight_Number: 'BF 123'
             Speed: 515.2000
           Heading: 90
        Passengers: 154
              Crew: {'Behnam'  'Jane'  'Jill'  'Ning'}
       Destination: 'Montreal'
              Feet: 22000
Latitude_Longitude: [50 100]

>> airspace(2)

ans = 

     Flight_Number: 'VSO 456'
             Speed: 99.6000
           Heading: 270
        Passengers: 270
              Crew: {'Frances'  'Jake'  'Jaiwei'}
       Destination: 'Los Angeles'
              Feet: 21000
Latitude_Longitude: [50 80]

>> airspace(3)

ans = 

     Flight_Number: 'BF 8421'
             Speed: 1.5057e+03
           Heading: 170
        Passengers: 0
              Crew: {'Giuseppe'  'Susan'}
       Destination: 'Calgary'
              Feet: 33000
Latitude_Longitude: [60 80]

>> airspace(4)

ans = 

     Flight_Number: 'AB 896'
             Speed: 500
           Heading: 90
        Passengers: 132
              Crew: {'Hao'  'Ashraf'  'Sue'}
       Destination: 'Montreal'
              Feet: 33000
Latitude_Longitude: [50 100]

So what I am trying to do is to iterate through the names of the airspace, and if it equals the name given by the input argument of the function, then it will find where the name occurs in the structure array and add on the passenger numbers, and then divide number of times the name was used, to get the average. My incomplete code is below:

function output = passavg_structure(airspace,name)
output = 0;
[a, b, c, d] = airspace(:).Destination;
for k = {a,b,c,d}
if strcmp(k,name) == 1
    output = airspace().Passengers;
else 
    output = 0;
end 
output = output/;
end

Im lost on how to find the loaction as to where the matched name occurs in the structure array. I feel like that is my problem with my code and thereore it is incomplete since I'm not sure on how to do that.

For example, lets say we did:

passavg_structure(airspace,'Montreal')

The output would be = 143, since it added the passengers from where Montreal occured and divided it by 2, since Montreal occured twice.

Any help on this would be greatly appreciated!

Thank you all.

2 Answers 2

1
mean([airspace(strcmpi({airspace(:).Destination},'Montreal')).Passengers])

From inside out

  1. Get field Destination (returns comma separated list)
  2. Put csv it into a cell, as it contains strings
  3. use strcmpi to get a logical index of matching entries
  4. use the logical index to get the substruct which contains all data with destination montreal
  5. Put csv in a array as, as it contains numeric data
  6. Apply mean

If no data exits, mean returns NaN

Sign up to request clarification or add additional context in comments.

Comments

1

You can use ismember to solve quite directly your problem:

function out = passavg_structure(airspace, name)

out = mean([airspace(ismember({airspace(:).Destination}, name)).Passengers]);

and out contains the average number of passengers to destination name.

Best,

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.