How to check for size of input vector
31 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(2 个)
Rik
2020-10-26
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 个评论
Rik
2020-10-26
What are you providing as input for V and what is the exact error? (put clc at the top and copy all text)
VBBV
2022-3-11
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Function Creation 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!