Storing Data from While loop

1 view (last 30 days)
shobhit mehrotra
shobhit mehrotra on 25 Jan 2015
Edited: Stephen23 on 25 Jan 2015
Hello, I'm trying to create a matrix with the outputs of a while loop. Attached is an image which will help visualize what I'm doing. The y-axis is Y, and the x-axis is i.
Every time the y-value is at 4 I want to evaluate a function of those indices and store in a matrix. When the function is at 5 and returns to 4 I'd like to create a new index in the matrix and evaluate the function.
heres what i have
for i = (25600:26000)
while y == 4
for n = 1:50
A(n) = log (i)
else
A (n) = A (n+1)
end
end
Thanks for your help!
  1 Comment
Stephen23
Stephen23 on 25 Jan 2015
Do not use i (or j) as the names of your variables, as these are the names of the inbuilt imaginary unit .

Sign in to comment.

Accepted Answer

Stephen23
Stephen23 on 25 Jan 2015
Edited: Stephen23 on 25 Jan 2015
When I copy-and-paste your code into the MATLAB Editor it indicates two major syntax errors. Please use the Editor and use the syntax checking tool and pay attention to the highlighting and lines where error messages are displayed.
It looks like you might be using the syntax from another language, because the for , while and else statements do not have the required matching end statements. You really need to read the documentation for all of these operations, as the usage is quite different to what you have in your code.
In any case you should avoid using loops for this kind of problem, and learn to use indexing properly. You also need to read about vectorization . Using MATLAB's powerful indexing and vectorizing the operations is what makes MATLAB code neat and fast. Learn to use them!
For example the log operation can be applied to all elements of a vector in one go:
vec = 25600:26000;
A = log(vec);
You also need to check the documentation for log and log10: did you actually mean to use log10 ?
You do not give any data or information about the "function" that you refer to, which makes it difficult to show you how you can check its values.
What do you mean by "create a new index in the matrix" ?

More Answers (0)

Tags

Products

Community Treasure Hunt

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

Start Hunting!

Translated by