How to create a for loop
1 view (last 30 days)
Show older comments
How can i create a for loop that will give me seperate column vectors. I have a row vector, which i want to be the end values of the column vectors. Due to the data being loaded there will be varying values in the row vector and this is why i have used the command to determine c. I have defined the variable xstart to be a row vector containing just 0's for the same amount of columns as xend has. Therefore i have starting and finished values. I need to get the output of the for loop to be x1=[], x2=[], ... , xc=[]
So far my script looks like:
x=input('define independant variable (put in ''):');
y=input('define variable to be smoothed (put in ''):');
%These two lines as user to define the variables x and y based on the %data they have in their workspace
[r,c]=size(x);
%determines how many rows (r) and columns (c) are in matrix x
xend=x(end,:);
%creates a vector which contains only the final row in matrix x (this %should be the final time recording for the viable cell count)
xstart=zeros(1,c);
for n=1:c;
*xn=(xstart(1,n):0.5:xend(1,n))';*
end
Thank you
0 Comments
Answers (1)
Jan
on 22 Feb 2014
It is a bad idea to create a bunch of variables with an index hidden in the name. Using komplicated methods to create variables dynamically demands for other complivated methods to access them later. So better use an array with an index to create a bunch of data:
x = cell(1, c);
for n=1:c
x{n} = xstart(1,n):0.5:xend(1,n))';
end
Now x is a cell array and accessing them is fast, nice and easy.
0 Comments
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!