How to loop through cell array and apply script to each double?

21 次查看(过去 30 天)
Hi,
I have a cell array of cell arrays of doubles called "C_512_numeric". I have the script called edit and I want to loop through all columns in the doubles in C_512_numeric and use the column data as input for edit (first argument). I have attached the script.
How would I write this code?

回答(2 个)

Aquatris
Aquatris about 22 hours 前
编辑:Aquatris about 22 hours 前
You can use the cellfun function, where you use your script as the function. Example:
a = {1:3, 200:205, 10:12};
new_a = cellfun(@(x) myfun(x) ,a,'UniformOutput',false);
new_a{:}
ans = 1x3
-2 -4 -6
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
ans = 1x6
-400 -402 -404 -406 -408 -410
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
ans = 1x3
-20 -22 -24
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
function y = myfun(x)
y = x*(-2);
end
  1 个评论
lil brain
lil brain about 21 hours 前
I dont think I quite understand. I am not that familiar with cellfun. Could you kindly show me the script?

请先登录,再进行评论。


Star Strider
Star Strider about 21 hours 前
The only code in your ‘edit.m’ file is the ‘dfaedit’ function that wants as arguments ‘file_name’, ‘plot_flag’, ‘outfile_flag’, and ‘out_command_flag’. There are no file names here other than the .mat file name (at least that I can see), and I need to know what you want to put for the otther arguments, since that’s not obvious to me at this point.
I also don’t understand what the function is suppsed to do, since it doesn’t seem to actually read or load the file, and instead starts by counting the number of characters in the file name.
What do you want to do with the ‘C_512_numeric.mat’ file?
LD = load('C_512_numeric.mat')
LD = struct with fields:
C_512_numeric: {7x4 cell}
Datac = LD.C_512_numeric
Datac = 7x4 cell array
{11x1 cell} { 6x1 cell} {14x1 cell} {15x1 cell} { 7x1 cell} {10x1 cell} { 7x1 cell} { 5x1 cell} {10x1 cell} {12x1 cell} {10x1 cell} { 7x1 cell} { 7x1 cell} { 4x1 cell} { 5x1 cell} { 4x1 cell} {21x1 cell} {10x1 cell} {12x1 cell} { 6x1 cell} { 6x1 cell} { 6x1 cell} { 7x1 cell} { 5x1 cell} { 4x1 cell} { 4x1 cell} { 5x1 cell} { 4x1 cell}
Datac{1}
ans = 11x1 cell array
{512x2 double} {512x2 double} {512x2 double} {512x2 double} {512x2 double} {512x2 double} {512x2 double} {512x2 double} {512x2 double} {512x2 double} {320x2 double}
Datam{1} = cell2mat(Datac{1})
Datam = 1x1 cell array
{5440x2 double}
figure
plot(Datam{1}(:,1), Datam{1}(:,2))
grid
xlabel('Column 1')
ylabel('Column 2')
function [H]=dfaedit(file_name,plot_flag, outfile_flag, out_command_flag)
% DFA Calculates and plots detrended fluctuations
%
% For a 1-d time series in a one column text file named 'file_name',
% DFA performs a detrended fluctuation analysis on this data. It first integrates
% the data and then partitions it into different size subsets whose size
% varies from MinBox (default = 4) to MaxBox (default = 1/4 the data length).
% For the subsets of each size, the best fitting trend lines are found
% and their the root mean square residual (Q) is calculated. The log10
% of this fluctuation quantity (Q) is plotted against log10 of the points in a subset.
% The slope of this regression estimates the Hurst exponent (H). The fractal
% dimension can be calculated as 2-H.
%
% If plot_flag = 1 then a regression plot is created. If outfile_flag = 1, the file name, H
% and r^2 are appended to a text file 'dfa_results.txt'. If out_command_flag = 1, the data
% that are the basis for the regression plot are output to the command window.
%
% Example: dfa('noise-f.dat', 1, 1, 1)
%
% I used the Peng's C code algorithm which is available at http://reylab.bidmc.harvard.edu.
% It is discussed in Peng et al. Quantification of scaling exponents and crossover
% phenomena in nonstationaryheartbeat time series. Chaos 1995;5:82-87. I vectorized the
% preliminary calculations for MatLab. But the main loop I did not touch. Still not sure
% of the algorithm they are using to calculate the root mean square residual.
%
% Syntax:
% dfa(file_name,plot_flag, outfile_flag, out_command_flag)
%
% R. C. Schmidt 11-27-01
%-------------------------------------------------------------------------------------
format short g
x = file_name;
numberpoints = length(x);
MinBox = 4; % minbox #of points in a box
MaxBox = .25*numberpoints; % maxbox set 1/4 the data length
BoxSizeDensity = 4;
LogScaleFactor = power(2.0, 1.0/BoxSizeDensity);
index = 1:numberpoints;
%%
% Preliminary calculations
Sum = cumsum(x);
SumOfSumsSquared = cumsum(Sum.*Sum);
SumOfSums = cumsum(Sum);
SSc = cumsum(index.*Sum);
% now find best fit lines and find fluctuation about the line
% loop for each box size, from MinBox to MaxBox
iteration = 1;
BoxSize = MinBox;
TempBoxSize = MinBox;
while BoxSize <= MaxBox
s = 0.0;
r1 = 1./(BoxSize + 1.0);
Det = 12.0*r1*r1*r1/(1.0 - r1*r1);
vv = BoxSize*(2.0*BoxSize + 1.0)/6.0;
for j = 2:numberpoints - BoxSize
dhh = SumOfSumsSquared(j + BoxSize) - SumOfSumsSquared(j-1);
dhu = SumOfSums(j + BoxSize) - SumOfSums(j-1);
dhv = SSc(j + BoxSize) - SSc(j-1) - dhu*j;
s = s + dhh - (dhv*dhv +dhu*dhu*vv - dhv*dhu*BoxSize)*Det;
j = j + 1;
end
den = (numberpoints - BoxSize)*(BoxSize + 1.0);
Fluctuation = sqrt(s/den);
log_points_in_box(iteration,1) = log10(BoxSize);
log_Q(iteration,1) = log10(Fluctuation);
show_results(iteration,:) = [ iteration BoxSize Fluctuation log_points_in_box(iteration,1) log_Q(iteration,1) ];
iteration = iteration + 1;
% update the box size
TempBoxSize = TempBoxSize*LogScaleFactor;
while round(TempBoxSize) < BoxSize + 1.0
TempBoxSize = TempBoxSize*LogScaleFactor;
end
BoxSize = round(TempBoxSize);
end
%%
% got all boxes; now calculate H via trendline
r_trend = corrcoef(log_points_in_box, log_Q);
coefs = polyfit(log_points_in_box, log_Q,1);
r_line = polyval(coefs,log_points_in_box);
% calculate dimension and Hurst
H = coefs(1);
D = 2-H;
%display results in command window
if out_command_flag == 1
disp(' iteration points Q Log10(pnts) Log10(Q)')
disp(show_results)
end
% display results in a figure
if plot_flag == 1
figure
h = axes('Position', [0 0 1 1], 'Visible', 'off');
axes('Position',[.1 .2 .75 .75])
plot(log_points_in_box, log_Q,'b+')
hold on
plot(log_points_in_box,r_line, 'k')
ylabel('log10(Q)');
xlabel('log10(Points in Subset)');
title(file_name);
set(gcf, 'CurrentAxes', h);
str(1) = {[sprintf('%2.5f',coefs(1)),'x + ', sprintf('%2.5f',coefs(2)), ' = y, r^2 = ', sprintf('%2.2f',r_trend(1,2)^2), ', n = ', sprintf('%d',length(log_Q)) ]};
text(.1, .02, str, 'FontSize', 10, 'Color', 'r');
str(1) = {['H = ', sprintf('%2.3f',H) ' D = ', sprintf('%2.3f',D) ]};
text(.5, .02, str, 'FontSize', 10, 'Color', 'r');
end
% append results to a file rd_results.txt
if outfile_flag == 1
fid = fopen('dfa_results.txt','a');
fprintf(fid,'%s %4.4f %2.4f\n', file_name, H, r_trend(1,2)^2);
status = fclose(fid);
end
end
.

类别

Help CenterFile Exchange 中查找有关 MATLAB Report Generator 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by