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에서 Data Type Conversion에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by