Info

Cette question est clôturée. Rouvrir pour modifier ou répondre.

How can I transform cell arrays into arrays that allow indexing?

1 vue (au cours des 30 derniers jours)
Wouter Wizard
Wouter Wizard le 7 Nov 2019
Clôturé : MATLAB Answer Bot le 20 Août 2021
Hi all,
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.
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
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?
I have appended the .m file and the arrays to this question. Thank you a lot for considering to provide me guidance in this problem!

Réponses (1)

Sammit Jain
Sammit Jain le 29 Jan 2020
Hi Wouter,
From what I can gather based on your query, you intend to remove ("subtract"?) certain rows from your data, based on a list of countries. So for example, you would like to eliminate the data in the cell array that corresponds to 'Belgium' because it appears on your list.
There doesn't appear to be a direct way to do this. However, you may write a small script to automate this. You may want to work with tables and categorical variables for this one, as they are easier to manage, especially for the operations you're looking to accomplish.
  1. Tables: https://www.mathworks.com/help/matlab/ref/table.html
  2. Categorical Variables: https://www.mathworks.com/help/matlab/categorical-arrays.html
As a simple fix, you may loop through your list of countries (possibly using a for loop) and then use the method described here to find matches (https://www.mathworks.com/matlabcentral/answers/386171-match-a-categorical-array-in-a-loop). Once a match is found, you can remove the rows from the cell arrays.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by