How to transpose cell arrays within a larger cell array

5 views (last 30 days)
Hi, I was wondering how to use cellfunc to find and transpose cell arrays that are row orientated within another cell array. I can do it using a loop but I was curious how to use cell function.
Code is below, I think there is a flaw in the method because I am using a loop mentality using seperate variables.
The dynamics file is a {1x3} cell and embedded within this are another set of {3x1} cells with uniform matrices. The third cell is the one that is row and ebThe 3rd cell is the one I can change, but I still think that on a larger scale my method will cause probelms.
If someone could have a quick it would be appreciated.
Thanks
load('dynamics.mat')
%find rows in cell array
idx = find(cellfun(@isrow, dyn));
trans_dyn = (cellfun(@transpose, dyn(idx), 'UniformOutput', false));
dyn(idx) = trans_dyn;

Accepted Answer

Jan
Jan on 31 Mar 2021
Edited: Jan on 31 Mar 2021
"The dynamics file is a {1x3} cell and embedded within this are another set of {3x1} cells with uniform matrices." - What are "uniform matrices?
"I still think that on a larger scale my method will cause probelms." - Why do you assume this?
idx = cellfun(@isrow, dyn); % Omit the FIND for faster logical indexing
dyn(idx) = cellfun(@transpose, dyn(idx), 'UniformOutput', false);
Or easier in 1 step:
dyn = cellfun(@(c) c(:), dyn, 'UniformOutput', false)
All these methods are slow.
Data = load('dynamics.mat');
dyn0 = repmat(Data.dyn, 1, 1e5); % A larger set
dyn = dyn0;
tic;
idx = find(cellfun(@isrow, dyn));
trans_dyn = (cellfun(@transpose, dyn(idx), 'UniformOutput', false));
dyn(idx) = trans_dyn;
toc % Elapsed time is 1.827274 seconds.
dyn = dyn0;
tic;
idx = cellfun(@isrow, dyn); % Omit the FIND for faster logical indexing
dyn(idx) = cellfun(@transpose, dyn(idx), 'UniformOutput', false);
toc % Elapsed time is 1.794807 seconds.
dyn = dyn0;
tic;
dyn = cellfun(@(c) c(:), dyn, 'UniformOutput', false);
toc % Elapsed time is 3.947908 seconds.
dyn = dyn0;
tic;
dyn = cellfun(@(c) reshape(c, [], 1), dyn, 'UniformOutput', false);
toc % Elapsed time is 2.599826 seconds.
dyn = dyn0;
tic;
for k = 1:numel(dyn)
dyn{k} = dyn{k}(:);
end
toc % Elapsed time is 1.523816 seconds.
dyn = dyn0;
tic;
for k = 1:numel(dyn)
dyn{k} = reshape(dyn{k}, [], 1);
end
toc % Elapsed time is 0.515385 seconds.
dyn = dyn0;
tic;
for k = 1:numel(dyn)
if isrow(dyn{k})
dyn{k} = dyn{k}(:);
end
end
toc % Elapsed time is 0.744554 seconds.
dyn = dyn0;
tic;
for k = 1:numel(dyn)
if isrow(dyn{k})
dyn{k} = reshape(dyn{k}, [], 1);
end
end
toc % Elapsed time is 0.433867 seconds.
dyn = dyn0;
tic;
for k = 1:numel(dyn)
if isrow(dyn{k})
dyn{k} = dyn{k}.';
end
end
toc % Elapsed time is 0.392983 seconds.
Prefer loops. cellfun is nice, but slower than a loop.
  1 Comment
Richard Rees
Richard Rees on 31 Mar 2021
Hi Jan, my code creates consistent sized files of parameters extracted from datafiles that are based on a known number of Node and Gau points from which they are derived. The dynamics is on the of the variables I create that is based on derviations of this data.
I am very rusty with cell functions, prefer loops for security.
Many thanks
Richard

Sign in to comment.

More Answers (0)

Categories

Find more on Performance and Memory in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!