Storing Data from While loop

조회 수: 2 (최근 30일)
shobhit mehrotra
shobhit mehrotra 2015년 1월 25일
편집: Stephen23 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
Stephen23
Stephen23 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 .

댓글을 달려면 로그인하십시오.

채택된 답변

Stephen23
Stephen23 2015년 1월 25일
편집: Stephen23 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개)

카테고리

Help CenterFile Exchange에서 Data Type Identification에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by