Save the values of a function in a for loop

I need to record the y-values of a line from x=a to x=b.
Each loop, the slope of the line will change so there will be a different set of x and y's for each loop
How can I record the y-values from the function for each loop? The above fix doesn't work if a function is inside of it. This is my code and gives me an error once it trys to record y(i):
function for_test
x = 0:1:10;
y = ones(size(x)) ;
for i=1:10
y(i) = x+rand;
y % use y(i) so that it is written as a vector
end
end

2 commentaires

The problem is this line
y(i) = x+rand;
The variable x is length 11
x+rand; % this would generate an output of length 11
However you are trying to assign it to a single value of y
y(i) % this is lenght 1
Therefore you are getting an error in assignment.
That is the exact error message I am getting but my question was how do I store each y values for each through each loop? I think I found a workaround, and that is to put the data into a cell array with {i}. Is that the proper fix or is there another way?

Connectez-vous pour commenter.

 Réponse acceptée

Hell Austin Hernandez,
you are correct. but Option01 is good.
Option 01:
function for_test
x = 0:1:10;
y = zeros(size(x)) ;
for i=1:10
y(i,:) = x+rand;
y % use y(i) so that it is written as a vector
end
end
Option 02:
function for_test
x = 0:1:10;
y = {}; %ones(size(x)) ;
for i=1:10
y{i} = x+rand;
y % use y(i) so that it is written as a vector
end
end

5 commentaires

Thank you!! This is a good fix. Now... the next step after that, I need to plot a graph based upon the answers given (there's a prompt window before this).
In my for loop, I have an if, elseif, and else statement, which gives 3 very different values. Should I try to put all these into 1 cell array? How can I access the data of each array and then plot it?
Or, if I do step 1 and construct a matrix for each of the 3 options. So if it were like:
h = 0:1:10
y = zeros(size(h));
v = zeros(size(h));
o = zeros(size(h));
prompts = {'enter 1'};
dlg = 'title';
c = 0;
m = 0;
u = 0;
hold on
for i=1:5
A = inputdlg(prompts, dlg);
Aa = str2double(A);
x = Aa(1);
if x == 1
y(i,:) = 1000*h;
m = m + 1;
plot(h,y(i,:));
elseif x == 0
v(i,:) = h+1;
u = u + 1;
plot(h,v(i,:));
else
o(i,:) = -h*5;
c = c + 1;
plot(h,o(i,:));
end
end
end
When I used Option 1, this method will generate a new row in the matrix of each output. So if I'm on loop 5, and the if statement is triggered, and I triggered the same statement in the very first loop, then I'll get a matrix like:
v =
1 2 3 4 5 6 7 8 9 10 11
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
1 2 3 4 5 6 7 8 9 10 11
How can I tell matlab that I want to access the rows that have nonzero elements? And then sort them from high to low (if the rows had different values)?
@ Austin
you are a fast learner. This can be fixed.
But please do one thing.
When you have a new bug to fix.
Then accept previous one and raise a new question.
This will help all 3 of us, myself, you and others.
Because search is always and mostly based on subject title.
Hello Austin,
Answer is in your problem only....
h = 0:1:10
y = zeros(size(h));
v = zeros(size(h));
o = zeros(size(h));
prompts = {'enter 1'};
dlg = 'title';
c = 0;
m = 0;
u = 0;
hold on
for i=1:5
A = inputdlg(prompts, dlg);
Aa = str2double(A);
x = Aa(1);
if x == 1
y(m,:) = 1000*h;
m = m + 1;
plot(h,y(m,:));
elseif x == 0
v(u,:) = h+1;
u = u + 1;
plot(h,v(u,:));
else
o(c,:) = -h*5;
c = c + 1;
plot(h,o(c,:));
end
end
end
thank you, I will do that
@ Austin,
Thank you
And, Initialize the values with One NOT Zero ...
c = 1;
m = 1;
u = 1;

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur MATLAB dans Centre d'aide et File Exchange

Produits

Version

R2020a

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by