MATLAB Answers

Storing Data from While loop

4 ビュー (過去 30 日間)
shobhit mehrotra
shobhit mehrotra 2015 年 1 月 25 日
編集済み: Stephen 2015 年 1 月 25 日
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 件のコメント
Stephen
Stephen 2015 年 1 月 25 日
Do not use i (or j) as the names of your variables, as these are the names of the inbuilt imaginary unit .

サインインしてコメントする。

採用された回答

Stephen
Stephen 2015 年 1 月 25 日
編集済み: Stephen 2015 年 1 月 25 日
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" ?

その他の回答 (0 件)

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by