can not figure this out?
조회 수: 2 (최근 30일)
이전 댓글 표시
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:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/761791/image.png)
댓글 수: 4
Walter Roberson
2021년 10월 8일
t1 = isequal(3,5)
class(t1)
t2 = isequal(3,5) == 1
class(t2)
t3 = isequal(5,5)
class(t3)
t4 = isequal(5,5) == 1
class(t4)
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)
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
댓글 수: 0
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!