Storing User Input as Vector, Stopping on 0 value for calculation.
Show older comments
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%User will be prompted to enter values to calculate %%
%%root mean square. Zero value will complete the while %%
%%loop and print to output. %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear all; clc; close all;
%Prompt user with script characteristics
disp('User will enter in values to calculate RMS.');
disp('Enter 0 to complete sequence.');
pause(1);
x=[]; %Initialize vector
i=0; %Vector interval
while 1
i=i+1; %Initialize interval
x(i) = input('Enter a value: '); %Prompt for input
if x(i) == 0; %Completes vector
break;
end;
end;
y=x(x~=0); %Removes zero value from vector
out = rms(y); %Intialize RMS variable
disp(y); %Displays vector values for user verification
fprintf('The RMS value of the inputs is %.4f.',out);
This is what I have so far, and I think it works. I know there has to be an easier way to do this, though, but I have to use a while loop for the homework. Any advice is appreciated.
1 Comment
dpb
on 2 Oct 2016
If the assignment is to use a loop and enter values one-at-a-time, looks ok to me...one minor stylistic nit would be you could make the fixup on the entered x vector in place instead of introducing another variable.
Also, while you disp the values, you don't offer any chance to correct or modify them before going on--that may be more than the assignment asked for, but just a comment.
Answers (1)
Jos (10584)
on 2 Oct 2016
The code look ok, but what I really like is that you have commented it quite well. Keep that up!!! Maintaining and debugging code in the future become peanuts when it is commented adequately :)
One suggestion is to add a value to after the check, using a temporary variable. Note that you do not need to have an iterator i and another variable y.
x= []
while 1
tmp = input ...
if tmp == 0
break
end
x(end+1) = tmp ; % automatically increases the size of x
end
2 Comments
jc12189
on 2 Oct 2016
dpb
on 2 Oct 2016
Alternatively, and while it's not a serious issue in a user interface where the number of elements is presumed to be going to be quite small plus the user input is much slower than computer speeds anyways, you could preallocate the array to a large size and then increment into it during the loop. Then just eliminate the end of the array past the used values when end. Again, not an issue here but the idea of preallocating in general code is one to pick up.
Also, another really (nearly) trivial nit--I'm in favor of the logical expression in the while actually being logical type.
while true
...
While (so to speak :) ) the numeric value of true is 1, it is class logical instead of double --
>> true
ans =
1
>> whos ans
Name Size Bytes Class Attributes
ans 1x1 1 logical
>>
Again, we're nitpicking here, granted...
Categories
Find more on Loops and Conditional Statements 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!