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.

 Respuesta aceptada

Jan
Jan el 26 de Jul. de 2011

1 voto

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 comentario

Katherine Anderson
Katherine Anderson el 26 de Jul. de 2011
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!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by