Search code examples
matlabindexingcell-array

Matlab - How can I transform a set of cell arrays into an array that allows indexing?


I am struggling with this problem for a day and cannot find a solution anywhere online. I have four cell arrays with data per country on which I perform operations to find a number of countries that I want to analyse. I have saved these countries in a 27x1 cell array with nonnumerical attributes whose output looks like:

'Belgium'
'Bulgaria'
'Croatia'
'Cyprus'
'Czechia'
'Denmark'
'Estonia'

This is an example of the rows that I want to subtract from other cell arrays with data per country. The problem is that cell arrays do not allow indexing which means that I cannot use these to subtract data from other cell arrays. So what I want as output is an array that allows indexing such that I can use that array to subtract information of other cell arrays.

What I have tried:

  • I have tried str2double to create rows that allow indexing. This resulted in NaN values which did not allow any operation
  • I have tried cell2mat which gave the error: Dimensions of arrays being concatenated are not consistent.
  • I have tried to create a table from cell arrays, but I couldent paste all the data in it from the different cell arrays because I couldent subtract it

I am new here so I dont know how I can append my .m file and cell arrays. Therefore, I add a part of my code here:

[~,ia,ib] = intersect(pop(:,1),gdp(:,1));
Com_popgdp = [pop(ia,1:2),gdp(ib,2)];

[~,ia,ib] = intersect(fp(:,1),lr(:,1));
Com_fplr = [fp(ia,1:2),lr(ib,2)];

[~,ia,ib] = intersect(Com_popgdp(:,1),Com_fplr(:,1));
Com_all = [Com_popgdp(ia,1:2),Com_fplr(ib,2)]; 

Com_all = Com_all(:,1);

%Com_all is the resulting cell array with all countries that I want to
%analyse resulting from the intersections of cell arrays. For the analysis, 
%I must extract the Com_all rows from
%pop/gdp/fp/lr. However, this is not possible with cell arrays. How can I
%access and extract the rows from pop/gdp/fp/lr for my analysis?

Could anyone help me find a way in which I can use the selection cell arrays as indexing to subtract data from other cell arrays? Which method would be appropriate?


Solution

  • It seems to me that you first want to compute the intersection of all lists of country names, then index the cell arrays accordingly. intersect finds the intersection of two lists, you can call it multiple times in sequence to intersect multiple lists. And ismember finds which of the selected countries is present. For example:

    A1 = {
    'Bulgaria',2
    'Croatia',3
    'Cyprus',4
    'Czechia',5
    'Denmark',6
    'Estonia',7
    };
    
    A2 = {
    'Belgium',11
    'Bulgaria',12
    'Croatia',13
    'Cyprus',14
    'Denmark',16
    'Estonia',17
    };
    
    A3 = {
    'Belgium',21
    'Croatia',23
    'Cyprus',24
    'Czechia',25
    'Denmark',26
    'Estonia',27
    };
    
    [countries] = intersect(A1(:,1),A2(:,1));
    [countries] = intersect(countries,A3(:,1));
    i1 = ismember(A1(:,1),countries); A1 = A1(i1,:);
    i2 = ismember(A2(:,1),countries); A2 = A2(i2,:);
    i3 = ismember(A3(:,1),countries); A3 = A3(i3,:);
    A = [A1,A2(:,2),A3(:,2)];
    

    The code above does assume that the three input cell arrays all have the countries in the same order. If this is not the case, sort the arrays using sort before matching them up with the list of selected countries:

    i1 = ismember(sort(A1(:,1)),countries);