find the largest array
3 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Image Analyst
2012-4-2
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)
更多回答(1 个)
John D'Errico
2012-4-3
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 个评论
Jan
2012-4-3
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".
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrices and Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!