Display Values That Satisfy a While Condition
1 view (last 30 days)
Show older comments
Hi All,
I have a function that calculates and displays a matrix based on some input variables, this matrix has to be real and to contain zeros in some cells. So I use a while loop to iterate through possible values until I find the good ones. My code is a little long but looks like this:
function Z = myfunction(n,p)
check = 0;
while check == 0;
... % Generate a matrix
if isreal(S) % Check whether matrix is real
if ~all(S(:)) % Check whether matrix contains zeros
check = 1;
end
end
Z = S;
I would like to see the values for which my conditions are satisfied. How can I do that? I tried:
disp(v);
at the end of the function, but it's not working.
Thanks a lot in advance.
2 Comments
Walter Roberson
on 17 May 2015
What is "v" in this context? Which "values" are you trying to display? Is screen output okay as it goes along, or does the information have to be returned back out of myfunction?
Walter Roberson
on 17 May 2015
You might prefer the cleaner code
while true
... %generate the matrix
if isreal(S) & any(S(:) ~= 0)
break; %leave the loop
end
end
%we got here because of break
Answers (0)
See Also
Categories
Find more on Matrices and Arrays 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!