1

I'm being given the following information:

Matrix with given information

where "routenummer" stands for roadnumber, "gemeente" for city, "afstand" for distance in km and "moeilijkheidsgraad" for difficulty.

I need to create a matrix titled difficultRoads with all of the given information of the walking roads with a difficulty lower than 3. To make this specific matrix I need to use matrix routes (which I've made in my code and consists of the original matrix without the information about the cities).

n = 0;

nr = [1; 2; 3; 4; 5; 6; 7; 8]
afst = [13; 13; 9.5; 4.5; 11; 12; 16; 8]
gr = [1; 3; 2; 2; 3; 3; 3; 2] 

routes = zeros(8, 3);
routes(:,1) = nr;
routes(:,2) = afst;
routes(:,3) = gr;

routes
for r = 1:8
    if routes(r,2) > 10 
        n = n + 1;
    end
end
for r = 1:8
    sel= (routes(r,3) < 3)
    % I have no clue on what to do here, gr(sel) 
    % doesn't seem to work and I have no idea how 
    % I could turn it into a matrix aswell with 
    % only the extra information of the required roads.
end        
disp(['The number of roads with a distance bigger than 10 km is ' num2str(n)])

As you can see, I also created the vectors nr, afst, gr and the matrix routes. That is an other part of the assignment and can be ignored.

When runned a matrix should be formed like this

1          Merelbeke            13         1
3          Kluisbergen          9.5        2
4          Kruishoutem          4.5        2
8          Oudenaarde           8          2

Thanks in advance!

rayryeng
  • 102,964
  • 22
  • 184
  • 193
Nicolas
  • 89
  • 1
  • 8

1 Answers1

1

If I understand you correctly, you want a subset of this matrix where it lists all of the roads that have difficulty less than 3. That can simply be done by:

difficultRoads = routes(routes(:,3) < 3, :);

BTW, if I can make one comment with your code, you can create that matrix by simply doing:

nr = [1; 2; 3; 4; 5; 6; 7; 8]
afst = [13; 13; 9.5; 4.5; 11; 12; 16; 8]
gr = [1; 3; 2; 2; 3; 3; 3; 2]     

routes = [nr afst gt];

There's no need to pre-allocate here and populate each column by itself. Simply create the matrix by stacking the columns together horizontally.


Back to your original problem, this uses what is known as logical indexing. Basically, we take a look at the last column of routes and determine all of those difficulties that are less than 3. This is done by routes(:,3) < 3. This produces a logical matrix where true / 1 satisfies the condition and false / 0 does not. You would then use this matrix to access the rows of your matrix routes and extract out those rows that satisfied the condition, hence getting that result.

However, what you have written is only the numerical data. You'd also like to display the cities too.

We can create a table structure to help us do that but first, you're going to need to define the cities which you haven't done yet.

%// Your data + the cities
nr = [1; 2; 3; 4; 5; 6; 7; 8]
afst = [13; 13; 9.5; 4.5; 11; 12; 16; 8]
gr = [1; 3; 2; 2; 3; 3; 3; 2]     

routes = [nr afst gt];
cities = {'Merelbeke', 'Oudenaarde', 'Kluisbergen', 'Kruishoutem', 'Maarkedal', 'Maarkdeal', 'Luisbergen', 'Oudernaarde'}.';

Once you do that, form a table:

T = table(nr, cities, afst, gr, 'VariableNames', {'Routenummer', 'Gemeente', 'Afstand', 'Moeilijkheidsgraad'});

Note that I'm also giving each column its own name, because if you don't it just defaults to what the variable name is called.

We get:

T = 

    Routenummer      Gemeente       Afstand    Moeilijkheidsgraad
    ___________    _____________    _______    __________________

    1              'Merelbeke'       13        1                 
    2              'Oudenaarde'      13        3                 
    3              'Kluisbergen'    9.5        2                 
    4              'Kruishoutem'    4.5        2                 
    5              'Maarkedal'       11        3                 
    6              'Maarkdeal'       12        3                 
    7              'Luisbergen'      16        3                 
    8              'Oudernaarde'      8        2     

Now that you have this, index into this table instead:

difficultRoads = T(routes(:,3) < 3, :);

We get:

difficultRoads = 

    Routenummer      Gemeente       Afstand    Moeilijkheidsgraad
    ___________    _____________    _______    __________________

    1              'Merelbeke'       13        1                 
    3              'Kluisbergen'    9.5        2                 
    4              'Kruishoutem'    4.5        2                 
    8              'Oudernaarde'      8        2  
rayryeng
  • 102,964
  • 22
  • 184
  • 193
  • Thank you! It does the trick, but I would also need to include the cities in the second column. Any ideas? – Nicolas Oct 28 '15 at 16:44
  • Great answer! Thank you very much for your help. – Nicolas Oct 28 '15 at 16:49
  • @Nicolas No problem at all :) BTW, I've made the answer a bit better. I've included the original tiles for each column in the table. Good luck! – rayryeng Oct 28 '15 at 16:52