For Loop for plotting different scatter plots

I have a dataset that I want to split into 24 different matrices, numbered 1 to 24 and I want to plot column 34 against column 7 of each of these matrices.
Part 1
for i = 1:1:24
group{i} = data(i:i+2,1:34)
i = i + 3
end
Part 2
for i = 1:1:24
hold on
figure
subplot(6,4,i)
scatter(group{i}(:,34),group{i}(:,7))
title (num2str(i))
xlabel('Moisture')
ylabel('Shear Strength')
end
I'm not sure where I'm going wrong. this is my first time trying for loops.

1 comentario

Amelia - why are you adding three to i in your first for loop?
i = i + 3
Your code to get the group data is
group{i} = data(i:i+2,1:34)
so it looks like you are trying to extract 3x34 matrices on each iteration of your loop. If this is the case, then you may want to try
k = 1;
for i = 1:1:24
group{i} = data(k:k+2,1:34)
k = k + 3;
end
where we use k to extract the data. Note that I'm assuming that data has enough rows to extract these 24 matrices.

Iniciar sesión para comentar.

Respuestas (1)

Jan
Jan el 30 de Ag. de 2017
Editada: Jan el 30 de Ag. de 2017
For the problem in Part 1 see Geoff's comment.
The part 2 creates 24 figures. Better move the figure command before the loop.
You can split the data without a loop:
dataGrouped = permute(reshape(data, 3, 24, 34), [1,3,2]);
Now dataGrouped(:, :, k) is the same as group{k}.

Categorías

Etiquetas

Preguntada:

el 30 de Ag. de 2017

Editada:

Jan
el 30 de Ag. de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by