How to check for size of input vector
10 views (last 30 days)
Show older comments
I am trying to get my code to ask a user to input a vector and then check to see if the vector is a 1 x 5 or not, and then if it is a 5 x 1 transpose it, and if its none then count chances + 1 and if they dont do it 3 times it is an error and the program terminates. can someone help?
A = [1 , 5]; % compare the size of vector entered by user is 1 X 5
B = [5 , 1]; % compare the size of vector entered by user is 5 X 1
chances = 0; %setting flag to use in loop
while chances<3
% V = input('Enter a 1 X 5 vector: '); %getting user input and adding it two v
s = size(V); %determining size of entered vector
if V(A,s)==1 % checking that size of v is 1 X 5
fprintf('The resulting 1 X 5 vector is: \n'); % assigning message to display
disp(V); %displaying v
chances=4; % setting termination condition for loop
elseif (V(s,B)==1) % checking that size of v is 5 X 1
warning('Error: You entered a 5 X 1 vector! \n'); % errror message displaying
V = V.'; % transposing vector to 1 X 5
fprintf('The resulting 1 X 5 vector is: \n'); % assigning message to display
disp(V); %displaying v
chances=4; % setting termination condition for loop
else
fprintf('Entered dimesion is wrong.\n');
chances = chances + 1; %incrementing chance count
end
end
if(chances==3)
error('Your Chances are over.'); % printing the error message if try is over
end
0 Comments
Answers (2)
Rik
on 26 Oct 2020
Start by uncommenting the line with input.
Next you need to think how to compare vectors. You can do something complicated, but you can also use isequal:
if isequal(s,A)
elseif isequal(s,B)
else
end
2 Comments
Rik
on 26 Oct 2020
What are you providing as input for V and what is the exact error? (put clc at the top and copy all text)
VBBV
on 11 Mar 2022
A = [1 , 5]; % compare the size of vector entered by user is 1 X 5
B = [5 , 1]; % compare the size of vector entered by user is 5 X 1
chances = 0; %setting flag to use in loop
while chances<3
V = input('Enter a 1 X 5 vector: '); %getting user input and adding it two v
s = size(V); %determining size of entered vector
if A == s % checking that size of v is 1 X 5
fprintf('The resulting 1 X 5 vector is: \n'); % assigning message to display
disp(V); %displaying v
chances=4; % setting termination condition for loop
elseif (s==B) % checking that size of v is 5 X 1
warning('Error: You entered a 5 X 1 vector! \n'); % errror message displaying
V = V.'; % transposing vector to 1 X 5
fprintf('The resulting 1 X 5 vector is: \n'); % assigning message to display
disp(V); %displaying v
chances=4; % setting termination condition for loop
else
fprintf('Entered dimesion is wrong.\n');
chances = chances + 1; %incrementing chance count
end
end
if(chances==3)
error('Your Chances are over.'); % printing the error message if try is over
end
Check with this
0 Comments
See Also
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!