Matrix with an entries field and need to grab category title
To achieve structure:


Country Name A


Reseller C

Country Name C


Reseller B
Reseller D



With configuration:

Category Countries

Channel Resellers:


rdCountry (Country Categories) field


Channel Products:


chooseResellers (Matrix) field


block


chooseReseller (Reseller Entries) field




You gonna need code like this:

{% for entry in craft.entries.slug(entrySlug).find() %}
    {% set countries = {} %}
    {% for block in entry.chooseResellers %}
        {% for reseller in block.chooseReseller.with(['rdCountry']) %}
            {% set countryName = reseller.rdCountry[0].title %}

            {% if countries[countryName] is defined %}
                {% set temp = countries[countryName] | merge([reseller]) %}
                {% set countries = countries | merge({(countryName): temp}) %}
            {% else %}
                {% set countries = countries | merge({(countryName): [reseller]}) %}
            {% endif %}

        {% endfor %}
    {% endfor %} 

    {% for countryName, resellers in countries %}
        &lt;h3&gt;{{ countryName }}&lt;/h3&gt;
        {% for reseller in resellers %}
            &lt;p&gt;{{ reseller.title }}&lt;/p&gt;
        {% endfor %}
    {% endfor %}
{% endfor %}




Because of the current section/field/relation configuration we need to use countries temporary array to store Country => Reseller structure. 

.with(['rdCountry']) is what you call eager loading. It allows you to reduce sql queries, speed up the whole process. With eager loaded entries field you replace find() with index number.

countries looks like this at the end:

{  
   "Country Name A":[  
      {  
         "title":"Reseller C"
      }
   ],
   "Country Name C":[  
      {  
         "title":"Reseller B"
      },
      {  
         "title":"Reseller D"
      }
   ]
}