using for loop to read multiple column vectors

I want to use a for loop to read different column vectors which are mentioned with different fn data in attached file and then I want to plot several figures from the output of for loop. Main problem is I don't know how to import fn vector data multiple times in loop and replace it with next fn vector when code complete for previous loop.
I have already imported data from txt file by using import tool and named the columns with variable names. fn is a varible which has name fn 10, fn22, fn32.... these are 5 column vectors.

Antworten (1)

Matt J
Matt J am 12 Jun. 2020

0 Stimmen

11 Kommentare

shah nawaz
shah nawaz am 14 Jun. 2020
how can i find mean of multiple arrays and generate a new array of answers?
Why not as follows
fn=cell{5,1}
for i=1:5
fn{i}=import(___);
end
mean_fn=cellfun(@mean, fn)
Is there anyway to use cellfun(@mean for multiple vectors of array. code above gives me mean of 1 vector in my array only. Now I have modified code to :
%using importfile1.m function file to import data by cell arrays
file199 = importfile1('DMconfaf199',9805,29531);
file171 = importfile1('DMconfaf171',5045,14081);
file132 = importfile1('DMconfaf132',1028,3097);
file122 = importfile1('DMconfaf122',488,1443);
file110 = importfile1('DMconfaf110',104,307);
%%combinig arrays into 1 array
fn = {file199(:,3) file171(:,3) file132(:,3) file122(:,3) file110(:,3)};
Particle1 = {file199(:,1) file171(:,1) file132(:,1) file122(:,1) file110(:,1)};
Particle2 = {file199(:,2) file171(:,2) file132(:,2) file122(:,2) file110(:,2)};
mfn = cellfun(@mean,fn{1,1});
% have to compute mean and standard deviation and plot figures by using hold on as well as given below
% sfn = std2(fn); % standard deviation
% figure(3)
% hold on
% y2 = normpdf(mfn,mmfn,smfn);
% logy2 = log10(y2);
% plot(lgmfn,logy2, '.r', 'LineWidth' ,4);
% title('Log-log plots of the probability distributions of\newline normalized normal forces for nq=71, μ=0')
% xlabel('Log_1_0(fn/mfn)')
% ylabel('Log_1_0(P_n fn/mfn)')
% grid on
% hold on
% y2 = normpdf(mfn,mmfn,smfn);
% logy2 = log10(y2);
% plot(lgmfn,logy2, '.g', 'LineWidth' ,2);
% hold on
Matt J
Matt J am 14 Jun. 2020
Bearbeitet: Matt J am 14 Jun. 2020
You can do things like,
fn = {file199 file171 file132 file122 file110 };
mfn=cellfun( @(c)mean(c(:,3)), fn);
You could also take the means of all columns like so,
fn = {file199 file171 file132 file122 file110 };
mfn=cellfun( @(c)mean(c), fn, 'uni',0);
shah nawaz
shah nawaz am 14 Jun. 2020
cannot use this to find mean of all columns since my columns are not of same dimensions in array fn or in file arrays. in the above example i need mean values like
mfn = {52.03 53.75 46.57...} for all fn and standard deviation as well. Still wondering how will i plot them later
Matt J
Matt J am 15 Jun. 2020
Bearbeitet: Matt J am 15 Jun. 2020
That should not matter. For example,
>> fn={rand(4,3), rand(10,3)}
fn =
1×2 cell array
{4×3 double} {10×3 double}
>> mfn=cellfun( @(c)mean(c), fn, 'uni',0)
mfn =
1×2 cell array
{1×3 double} {1×3 double}
or
>> mfncol3=cellfun( @(c)mean(c(:,3)), fn)
mfncol3 =
0.5084 0.6062
shah nawaz
shah nawaz am 15 Jun. 2020
i get this error on using mfn=cellfun( @(c)mean(c), fn, 'uni',0)
Undefined function 'sum' for input arguments of type 'cell'.
Error in mean (line 116)
y = sum(x, dim, flag)/size(x,dim);
Error in @(c)mean(c)
Matt J
Matt J am 15 Jun. 2020
Bearbeitet: Matt J am 15 Jun. 2020
As you can see from my example, that should not happen if the contents fn{i} are numeric matrices. If you happen to have cells nested in there instead of numeric variables, however, then you can expect the error message shown.
shah nawaz
shah nawaz am 15 Jun. 2020
moving between matrices and cell arrays i am stuck at how to use fn values again and again in loop to find fn/it's mean value each time and plot. fn column divided by it's mean is shown by mfn here. The code you told me above worked for me now for calculating mean of each column of fn arry. Can you please have a look. Thanks..
[a,b,fn99,~,~]= importfile('DMconfaf199', 9805, 29531); % a = particle 1 for nq = 99 & b = particle 2 for nq = 99 sample
[g,h,fn22,~,~]= importfile('DMconfaf122', 488, 1443); % ~ operator ignores the function outputs.
[e,f,fn32,~,~]= importfile('DMconfaf132', 1028, 3097);
[c,d,fn71,~,~]= importfile('DMconfaf171', 5045, 14081);
[k,l,fn10,~,~]= importfile('DMconfaf110', 104, 307);
Particles = {a b c d e f g h k l};
fn = {fn10 fn22 fn32 fn71 fn99};
hold on
for i = {fn10 fn22 fn32 fn71 fn99} %for loop for different fn values in the column vectors above
sfn = cellfun(@(c)std2(c), fn); % standard deviation
mean_fn = cellfun(@(c)mean2(c), fn); % mean of whole column matrix
mean_fn = num2cell(mean_fn, 1);
sfn = num2cell(sfn, 1);
for i = 1:length(mean_fn)
mfn = cellfun(@divide, fn{1,1},mean_fn{1,1}, 'uni', 0); % fn/its mean
smfn = std2(mfn);
lgmfn = log10(mfn);
mmfn = mean2(mfn); %in order to fill normpdf function parametres we take mean again smfn = std2(mfn);
log_fn = log10(fn);
lmfn = log10(mfn);
hold on
figure(3)
box on
y2 = normpdf(mfn,mmfn,smfn);
logy2 = log10(y2);
plot(lgmfn,logy2, '.r', 'LineWidth' ,4);
title('Log-log plots of the probability distributions of\newline normalized normal forces for nq=71, μ=0')
xlabel('Log_1_0(fn/mfn)')
ylabel('Log_1_0(P_n fn/mfn)')
grid on
hold on
end
shah nawaz
shah nawaz am 15 Jun. 2020
i have attached the code file as well.
Matt J
Matt J am 16 Jun. 2020
Bearbeitet: Matt J am 16 Jun. 2020
You are not using the same variable 'i' for both the inner and outer loops loop counters. Additionally, you don't seem to be using the loop counters anywhere inside the loops.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Hilfe-Center und File Exchange

Produkte

Version

R2017b

Gefragt:

am 12 Jun. 2020

Bearbeitet:

am 16 Jun. 2020

Community Treasure Hunt

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

Start Hunting!

Translated by