For Loop No Idea!

So I'm not used to how you write programming code in Matlab, or really any language to be honest, so could someone tell me how to make this for loop work
for start = 1:VgStep:endPoint-VgStep
for step = VgStep:VgStep:endPoint
x1 = x(start:step);
y1 = y(start:step);
end
end
I think you can probably get the general idea of what I would like it to do..

6 comentarios

Albert Yam
Albert Yam el 30 de Jul. de 2012
The loop you are writing is looking for the vector 'x', elements from 'start' to 'step', and putting that into x1 (and same for y1). Are you sure that is what you want to do?
Ryan
Ryan el 30 de Jul. de 2012
All I got out of that is that you are trying to save some variables in a loop:
Try explaining your problem or goals instead of placing code that you don't understand enough to even ask a question about it.
What information are you trying to get from x and y using these start and step terms? Currently through the loop you are saving over x1 and y1 each time with the values in vectors x and y with the values of those vectors between your current values of start and step (the start:step) - did you want that?
Walter Roberson
Walter Roberson el 30 de Jul. de 2012
The loop as written is overwriting x1 and y1 on each iteration of the two loops.
What is your desired output?
Adam Parry
Adam Parry el 30 de Jul. de 2012
Editada: Adam Parry el 30 de Jul. de 2012
I rhink we are nearly getting there :)
What I have is some data that although a lot bigger looks as follows
x y
0 8908
1 908
2 7897
0 8908
1 8908
2 789789
0 8908
1 784732
2 89034
i decided to put x and y in to two seperate column vectors and then i wanted to split each of them so I had either
x1 = [0,1,2]
x2 = [0,1,2]
etc, and
y1 = [8908,32890382,2313]
etc. or
a whole matrix array, as in
x1 0 784394
1 34244
2 434234
or something like that
Essentially I have a lot of data that isn't nicely separated when I take the measurements, but on top of that the size of each x vector (i.e in the case above [0,1,2] can vary).
Is that understandable?
Adam Parry
Adam Parry el 30 de Jul. de 2012
could i use some kind of return command?
Walter Roberson
Walter Roberson el 30 de Jul. de 2012
return will likely not help.
"break" might possibly help, perhaps.

Iniciar sesión para comentar.

Respuestas (2)

Adam Parry
Adam Parry el 31 de Jul. de 2012

0 votos

Hello people kind enough to help.
I have got myself stuck again.
Apparently Matlab does not like this either
for i = 1:VgStep
for start = 1:VgStep:endPoint-VgStep
for step = VgStep:VgStep:endPoint
XX(i) = x(start:step);
YY(i) = y(start:step);
end
end
end
pops up with this error
In an assignment A(I) = B, the number of elements in B and I must be
the same.

2 comentarios

Adam Parry
Adam Parry el 31 de Jul. de 2012
Editada: Adam Parry el 31 de Jul. de 2012
x y Vg
0 8908 0
-1 908 0
-2 7897 0
0 8908 -1
-1 8908 -1
-2 789789 -1
0 8908 -2
-1 784732 -2
-2 89034 -2
That is more like what the data looks like sorry
and this is the rest of the code
x = A.data(:,1);
y = abs(A.data(:,2));
Vg = A.data(:,3);
minx = min(x);
MaxX = (-minx)+1;
VgMin = min(Vg);
VgStep = (-VgMin/1)+1;
endPoint = (VgStep*MaxX);
Adam Parry
Adam Parry el 31 de Jul. de 2012
sorry just realised I was using the wrong brackets, should have been
for i = 1:VgStep
for start = 1:VgStep:endPoint-VgStep
for step = VgStep:VgStep:endPoint
XX{i} = x(start:step);
YY{i} = y(start:step);
end
end
end

Iniciar sesión para comentar.

Categorías

Etiquetas

Preguntada:

el 30 de Jul. de 2012

Community Treasure Hunt

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

Start Hunting!

Translated by