This is a homework question I have been stuck on.
조회 수: 1 (최근 30일)
이전 댓글 표시
Write a MatLab function called problem5 that inputs, in this order, a row vector vec, and a positive integer n, and outputs a two-row matrix such that the first row is vec and the second row consists of every element of vec raised to the n power. Your function should suppress all outputs. (b)Let vec = [3, 4, 2, 8, 10] and n = 3 and call your function. Store its output as variable X. Have MatLab echo X .
댓글 수: 0
채택된 답변
Image Analyst
2016년 2월 14일
Hint: see this snippet
% Ask user for two floating point numbers.
defaultValue = {'45.67', '78.91'};
titleBar = 'Enter a value';
userPrompt = {'Enter floating point number 1 : ', 'Enter floating point number 2: '};
caUserInput = inputdlg(userPrompt, titleBar, 1, defaultValue);
if isempty(caUserInput),return,end; % Bail out if they clicked Cancel.
% Convert to floating point from string.
usersValue1 = str2double(caUserInput{1})
usersValue2 = str2double(caUserInput{2})
% Check for a valid number.
if isnan(usersValue1)
% They didn't enter a number.
% They clicked Cancel, or entered a character, symbols, or something else not allowed.
% Convert the default from a string and stick that into usersValue1.
usersValue1 = str2double(defaultValue{1});
message = sprintf('I said it had to be a number.\nI will use %.2f and continue.', usersValue1);
uiwait(warndlg(message));
end
% Do the same for usersValue2
You can also input a string that way, so the user can enter in a bunch of numbers, then use sscanf() or textscan() to parse it into a vector of doubles. Good luck.
댓글 수: 2
Image Analyst
2016년 2월 14일
Try input(). Put it into a while loop until the user enters some number to indicate he's done with the vector, like -999 or something. At each iteration use str2double() if you need to convert a string to a number.
추가 답변 (1개)
Walter Roberson
2016년 2월 14일
See input() .
Note that when you use input(), the user needs to type in [] around their input if you are having them input a numeric vector. If you want to get around that, you need to use the 's' option of input so the user is typing in a string, and then you need to convert the string to numeric such as with regexp() 'split' and str2double()
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Time Series Objects에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!