find the largest array
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Consider the following example:
clear all
data = {rand(1,5),rand(1,4),rand(1,4),rand(1,6)};
data1 = cellfun(@(x) cell(size(x)),data,'UniformOutput',0);
time1 = cellfun(@(x) cell(size(x)),data,'UniformOutput',0);
for i = 1:length(data1);
a1{i} = length(data1{i});
for ii = 1:a1{i};
data1{i}{ii} = rand(26,1);
time1{i}{ii} = rand(26,1);
end
end
data1{1}{3} = rand(32,1);
time1{1}{3} = rand(32,1);
%interpolation
for i = 1:length(data1);
a1{i} = length(data1{i});
for ii = 1:a1{i};
t{i}{ii} = time1{i}{ii}; %original time
p{i}{ii} = data1{i}{ii};%data
x=time1{1}{3};%altered time - this need to be the data with the most
%measurements
newD{i}{ii} = interp1(t{i}{ii},p{i}{ii},x);
end
end
With this script I am trying to interpolate the time series of a set of data onto another series which has the maximum number of measurements. So, from the example above I would like to replace the line
x = time1{1}{3};
with a command which finds the cell with the maximum number of measurements (in this case time1{1}{3}) which can then be used to calculate 'newD'. How would I go about doing this?
0 commentaires
Réponse acceptée
Image Analyst
le 2 Avr 2012
It might not be the most compact way, but hopefully it's understandable, or at least as understandable as you can be with cell arrays:
count = 1;
for cellNumber = 1 : length(time1)
% for each cell in the time1 cell array...
numberOfArraysInCell = length(time1{cellNumber})
ca = time1{1, cellNumber}
% Get the number of times for each cell.
for arrayNumber = 1 : numberOfArraysInCell
numberOfTimes(count) = size(ca{3}, 1);
count = count + 1;
end
end
% Report the max number of times.
maxNumTimes = max(numberOfTimes)
Plus de réponses (1)
John D'Errico
le 3 Avr 2012
Start to think as you should in MATLAB, using tools like cellfun to do the work for you instead of loops.
[maxcellsize,maxcellind] = max(cellfun(@numel,time1));
1 commentaire
Jan
le 3 Avr 2012
Or, my usual comment about CELLFUN:
max(cellfun('prodofsize', time1))
The documentation claims, that the string commands of CELLFUN are kept for backward compatibility only. But in fact they are "much" faster than using a function handle, when the processed cell is "large".
Voir également
Catégories
En savoir plus sur Matrices and Arrays dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!