find the largest array

3 次查看(过去 30 天)
Richard
Richard 2012-4-2
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?

采纳的回答

Image Analyst
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)
  2 个评论
Richard
Richard 2012-4-3
The problem that I am having is not finding the maximum number of times, but finding which cells correspond to that maximum number of times i.e. which cells have 'maxNumTimes' of measurements!
Richard
Richard 2012-4-3
solved it! thanks for your help

请先登录,再进行评论。

更多回答(1 个)

John D'Errico
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
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 CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by