can not figure this out?

조회 수: 4 (최근 30일)
michail popovits
michail popovits 2021년 10월 8일
답변: Kshitij Chhabra 2021년 10월 12일
need to find a script that
accepts any number (determined by the program user at run-time) of user-input 2-D force vectors (in component form, with x-, y- magnitudes AND force application point);
  • You might ask the user how many vectors will be input (which is easier to code), or
  • You can use a signaling mechanism - for example, ask the user whether there are more vectors to input, and continue if there is an affirmative response (which is more interesting and/or user-friendly).
this is what i have but i do not think this in right:
  댓글 수: 4
michail popovits
michail popovits 2021년 10월 8일
%Get a 1 X 3 vector and print the same
A = [1 3]; % to compare the size of vector entered by user is 1 X 3
B = [3 1]; % to compare the size of vector entered by user is 3 X 1
f = 0; %setting flag to use in loop
% loop untill our requirement(untill 3 try or when entring correct input)
while f<3
V = input('Enter a 1 X 3 vector: '); %getting user input and adding it two V
s = size(V); %determining size of entered vector
if (isequal(s,A)==1) % checking that size of V is 1 X 5
fprintf('The resulting 1 X 3 vector is: \n'); % assigning message to display
disp(V); % displaying V
f=4; % setting termination condition for loop
elseif (isequal(s,B)==1) % checking that size of V is 5 X 1
fprintf('Error: You entered a 3 X 1 vector! \n'); % errror message displaying
V = V.'; % transposing vector to 1 X 5
fprintf('The resulting 1 X 3 vector is: \n'); % assigning message to display
disp(V); %displaying V
f=4; % setting termination condition for loop
else
fprintf('Entrored dimesion is wrong!\n'); % printing error when user entry size is wrong
f = f + 1; %incrementing chance count
end
end
if(f==3)
fprintf('Your Chances are over!\n'); % printing the error message if try is over
end
Walter Roberson
Walter Roberson 2021년 10월 8일
t1 = isequal(3,5)
t1 = logical
0
class(t1)
ans = 'logical'
t2 = isequal(3,5) == 1
t2 = logical
0
class(t2)
ans = 'logical'
t3 = isequal(5,5)
t3 = logical
1
class(t3)
ans = 'logical'
t4 = isequal(5,5) == 1
t4 = logical
1
class(t4)
ans = 'logical'
Under what circumstances are you expecting isequal(s,A) to have a different class() or size() or value than isequal(s,A)==1 ?

댓글을 달려면 로그인하십시오.

답변 (1개)

Kshitij Chhabra
Kshitij Chhabra 2021년 10월 12일
Hi,
From my understanding you need to take variable amount of 1x3 array inputs from the user and are trying to store them. I've taken the liberty to assume that if the user enters 3 incorrect inputs, the program is terminated.
First Approach:
n=input("Enter Number of Values to be inputted: ");
Value=[];
%Get a 1 X 3 vector and print the same
A = [1 3]; % to compare the size of vector entered by user is 1 X 3
B = [3 1]; % to compare the size of vector entered by user is 3 X 1
f = 0; %setting flag to use in loop
% loop until our requirement(untill 3 try or when entring correct input)
i=0;
while(i<n)
while f<3
V = input('Enter a 1 X 3 vector: '); %getting user input and adding it two V
s = size(V); %determining size of entered vector
if (isequal(s,A)==1) % checking that size of V is 1 X 3
fprintf('The resulting 1 X 3 vector is: \n'); % assigning message to display
disp(V); % displaying V
Value=[Value;V]; % Appending V to the set of other inputs
break; % setting termination condition for loop
elseif (isequal(s,B)==1) % checking that size of V is 3 X 1
fprintf('Error: You entered a 3 X 1 vector! \n'); % errror message displaying
V = V.'; % transposing vector to 1 X 3
fprintf('The resulting 1 X 3 vector is: \n'); % assigning message to display
disp(V); % displaying V
Value=[Value;V]; % Appending V to the set of other inputs
break; % setting termination condition for loop
else
fprintf('Entrored dimesion is wrong!\n'); % printing error when user entry size is wrong
f = f + 1; %incrementing chance count
end
end
if(f==3)
fprintf('Your Chances are over!\n');
break; % printing the error message if trIES is over
end
i=i+1;
end
Second Approach:
Value=[];
%Get a 1 X 3 vector and print the same
A = [1 3]; % to compare the size of vector entered by user is 1 X 3
B = [3 1]; % to compare the size of vector entered by user is 3 X 1
f = 0; %setting flag to use in loop
% loop until our requirement(untill 3 try or when entring correct input)
i=true;
while(i==true)
while f<3
V = input('Enter a 1 X 3 vector: '); %getting user input and adding it two V
s = size(V); %determining size of entered vector
if (isequal(s,A)==1) % checking that size of V is 1 X 3
fprintf('The resulting 1 X 3 vector is: \n'); % assigning message to display
disp(V); % displaying V
Value=[Value;V]; % Appending V to the set of other inputs
break; % setting termination condition for loop
elseif (isequal(s,B)==1) % checking that size of V is 3 X 1
fprintf('Error: You entered a 3 X 1 vector! \n'); % errror message displaying
V = V.'; % transposing vector to 1 X 3
fprintf('The resulting 1 X 3 vector is: \n'); % assigning message to display
disp(V); % displaying V
Value=[Value;V]; % Appending V to the set of other inputs
break; % setting termination condition for loop
else
fprintf('Entrored dimesion is wrong!\n'); % printing error when user entry size is wrong
f = f + 1; %incrementing chance count
end
end
if(f==3)
fprintf('Your Chances are over!\n');
break; % printing the error message if trIES is over
end
% check=input('Enter 0 if you want to input more values, else enter any other number:');
if check ~= 0
i=false;
end
end
For both of the approaches all the values are stored in "Value" and can be accessed as :
Value(x,:)
Here it will return the input that was given at x iteration

카테고리

Help CenterFile Exchange에서 Language Fundamentals에 대해 자세히 알아보기

태그

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by