MATLAB Answers

1

Manipulating multidimensional array cell elements

Asked by Andy Millar on 30 May 2017
Latest activity Answered by Jan
on 2 Jun 2017
I have a 1x9 array with each cell containing a 10000x1 array of data points. I wish to "strip" every cell in the original 1x9 array of values <= 0, most likely by using this operator in conjunction with 'something'=NaN. In the end, there should be a 1x9 array with each cell containing (for example) an 8000x1 array of data points.
load ('Project Data.mat','Fx1')
load ('Project Data.mat','Fx2')
load ('Project Data.mat','Fx3')
load ('Project Data.mat','Fx4')
load ('Project Data.mat','Fx5')
load ('Project Data.mat','Fx6')
load ('Project Data.mat','Fx7')
load ('Project Data.mat','Fx8')
load ('Project Data.mat','Fx9')
AllForce={Fx1(:,2) Fx2(:,2) Fx3(:,2) Fx4(:,2) Fx5(:,2) Fx6(:,2) Fx7(:,2) Fx8(:,2) Fx9(:,2)};
My attempt involves something along the lines of AllForce(AllForce(:)<=0)=[], but I can't seem to figure out what I should replace the "AllForce(:)" part with.
I am open to any suggestions with this problem. Thanks for the help!

  2 Comments

It may be easiest to convert the entire thing to a matrix, process it, and then convert it back to a cell. That will make things much easier to code and debug IMHO.
I second Rik Wisselink's comment: you should always store your data in the simplest array possible that will hold that data, as this always simplifies the processing of that data. On very common mistakes beginners make is to try and write pointlessly complicated code to work around the fact that their data is structured vary badly. In this case it would likely be better to load all of the data into one ND numeric array, and then the processing will be trivial.

Sign in to comment.

Tags

2 Answers

Answer by Rylan Dmello on 1 Jun 2017

You can operate over arrays within a cell array using the cellfun function:
A = cell(1, 9); % Create a 1x9 cell array
A = cellfun(@(arr) rand(10, 1), A, 'UniformOutput', false); % Populate each cell with 10x1 double arrays
B = cellfun(@(arr) arr(arr > 0.5), A, 'UniformOutput', false); % Remove elements <= 0.5
The above example uses cellfun with MATLAB function handle syntax to populate and filter arrays within a cell array.

  0 Comments

Sign in to comment.


Answer by Jan
on 2 Jun 2017

Data = load('Project Data.mat');
C = cell(1, 9);
for k = 1:9
C{k} = Data.(sprintf('Fx%d', k))(:, 2);
end
AllForce = cat(1, C{:});
AllForce = AllForce(AllForce > 0));

  0 Comments

Sign in to comment.