Manipulating multidimensional array cell elements

조회 수: 2 (최근 30일)
Andy Millar
Andy Millar 2017년 5월 30일
답변: Jan 2017년 6월 2일
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
Rik
Rik 2017년 5월 30일
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.
Stephen23
Stephen23 2017년 5월 30일
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.

댓글을 달려면 로그인하십시오.

답변 (2개)

Aylin
Aylin 2017년 6월 1일
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.

Jan
Jan 2017년 6월 2일
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));

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by