find the largest array

2 ビュー (過去 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 Center および File ExchangeMatrices and Arrays についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by