MATLAB Answers

How to check for size of input vector

18 views (last 30 days)
Sean St Cyr
Sean St Cyr on 26 Oct 2020
Commented: Rik on 26 Oct 2020
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

Answers (1)

Rik
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
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)

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by