How to check for size of input vector

32 Ansichten (letzte 30 Tage)
Sean St Cyr
Sean St Cyr am 26 Okt. 2020
Beantwortet: VBBV am 11 Mär. 2022
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

Antworten (2)

Rik
Rik am 26 Okt. 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 Kommentare
Sean St Cyr
Sean St Cyr am 26 Okt. 2020
okay, i tried that before and its not working that way, if there any error in my code above?
Rik
Rik am 26 Okt. 2020
What are you providing as input for V and what is the exact error? (put clc at the top and copy all text)

Melden Sie sich an, um zu kommentieren.


VBBV
VBBV am 11 Mär. 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

Kategorien

Mehr zu Function Creation finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by