can not figure this out?

4 ビュー (過去 30 日間)
michail popovits 2021 年 10 月 8 日

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 件のコメント2 件の古いコメントを表示2 件の古いコメントを非表示
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 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 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)
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)
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 Center および File ExchangeLanguage Fundamentals についてさらに検索

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by