MATLAB Answers

I am having trouble saving large vectors inside of a nested loop. Can anyone help out? Thanks!

1 view (last 30 days)
Jonathan Salazar
Jonathan Salazar on 30 Nov 2017
Commented: Jonathan Salazar on 30 Nov 2017
I have a script that pulls data for strain values out of files. This code was given to me and I am having the hardest time trying to modify it. the j loop cycles over 30 samples of the same experiment so that we could calculate average. the i loop cycles over different materials being tested. the bb data is the data I am Interested in which is a 5000 by 1 vector. I simply need to calculate the standard deviation for each row of the 30 samples so that I have 5000 std's for each iteration of i and save it in a matrix (so that i have a 5000 by 56 matrix after code is completed). every time I try to create a new variable to create a matrix or even simply save the current std the variable is no where to be found and is not saved to the work space. Any help would be apreciated. thank you!
clearvars
dat = dlmread('thermal.dat');
temp = dat(:,1);
k = dat(:,2);
epsilon = dat(:,3);
for i = 1:56
files = dir(num2str(i));
nsample = numel(files(~strncmpi('.',{files.name},1)));
aa = []; cnt2 = 0;
for j = 1:nsample%for 7: [1 2 3 4 6:12 14:20]
aa = dlmread([num2str(i),'/',num2str(j),'/out'],'',1,0);
bb = aa(:,6); %initialize pxx
end
end

Answers (1)

KSSV
KSSV on 30 Nov 2017
clearvars
dat = dlmread('thermal.dat');
temp = dat(:,1);
k = dat(:,2);
epsilon = dat(:,3);
iwant = zeros(nsample,56) ; % initialize
for i = 1:56
files = dir(num2str(i));
nsample = numel(files(~strncmpi('.',{files.name},1)));
aa = []; cnt2 = 0;
for j = 1:nsample%for 7: [1 2 3 4 6:12 14:20]
aa = dlmread([num2str(i),'/',num2str(j),'/out'],'',1,0);
bb = aa(:,6); %initialize pxx
iwant(j,i) = std(b) ; % get standard deviation and save
end
end
  1 Comment
Jonathan Salazar
Jonathan Salazar on 30 Nov 2017
Thank you for the quick reply! this gives me a 56 by 2 matrix. I need to save all 5000 std's for each iteration of i. I believe i need to first save a 5000 by 30 vecter of bb then take the row-wise std to obtain a 5000 by 1 vector. I then want to place this in the ith column. Is there any way to do this? thanks!

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by