storing values in matrix

I have a script wherein I have two nested for loops. The first for loop goes into a series of five directories. The second for loop goes into files within the directories and performs a series of analyses on waveforms. I'm trying to store certain values each time the script makes a loop through one directory. There's something wrong with my syntax obviously because instead of creating a new column for each directory its just adding onto the column the new values for the subsequent directories. I'm having difficulties getting it to start a new column when it stores. Any help would be much appreciated! Here's an example of a value I'm wanting to store:
keep2=[]
for i=1:length(dirs)
for pp=1:length(files)
keep(:,pp)=(snr510(:,pp)>1)&(snr1015>1)&(snr1520)>1;
keep2=[keep2;keep(:,pp];
end
end
Again instead of storing the values each time I move through the loop into a new column it just adds onto keep2.

 Accepted Answer

Jan
Jan on 26 Jul 2011
Do the directories have the same number of files? Then a 3D-array would be fine:
keep = zeros(1, length(dirs), length(files)); % Pre-allocate!
for i=1:length(dirs)
for pp=1:length(files)
keep(:, i, pp) = (snr510(:,pp)>1) & (snr1015>1) & (snr1520)>1;
end
end
If the directories have a different number of files, you need a CELL:
keepC = cell(1, length(dirs));
for i=1:length(dirs)
keep = zeros(size(snr510, 1), length(files));
for pp=1:length(files)
keep(:, pp) = (snr510(:,pp)>1) & (snr1015>1) & (snr1520)>1;
end
keepC{i} = keep;
end

1 Comment

Thanks Jan! The cell seemed to work..although I missed the {i} when I first did it and had quite the time messing around with things. Now I have the values stored in the cells individually for each directory-I'm gonna go ahead and store that way from now on..although its created a few other complications in my script that I'll have to iron out..I appreciate the help!

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!