Using a function in ian inputdlg

조회 수: 3 (최근 30일)
Gina Barlage
Gina Barlage 2015년 6월 2일
댓글: Gina Barlage 2015년 6월 3일
This was the assignment given to me:
The script should request a vector of positive integers entered as a single line in an inputdlg box. The script should create a 1-by-2 vector b where b(1) is the sum of the odd numbers in A and b(2) is the product of the even numbers in A.
An example execution is as follows:
>> script26
The line entered in the inputdlg box is: [2 1 5 2 9 11 7]
b =
33 4
This I what I have so far:
function[] = script26()
prompt = {'Array Numbers'};
dlg_title = 'Script26';
num_lines = 1;
def = {'2 1 5 2 9 11 7'};
b = inputdlg(prompt,dlg_title,num_lines,def);
b(1) = sum(def(1:2:end))
b(2) = prod(def(2:2:end))
A = [b(1) b(2)]
It won't let me do the sum function. How do I make the function to pull out the odd numbers of an array put into the inputdlg box??

답변 (2개)

Geoff Hayes
Geoff Hayes 2015년 6월 2일
Gina - the inputdlg function returns a cell array, so you will have to convert this into the appropriate format before trying to manipulate the data. For example,
response = inputdlg(prompt,dlg_title,num_lines,def);
data = str2num(char(response));
We convert the cell array to a string using char and then the string of characters to a numeric array using str2num. (Note that how we convert on response and not the default values of def as in your example.)
Now that you have an array of numbers, you will need to iterate over each one and determine if it is odd or even. If the former, then add it to your odd sum. And if the latter, then multiply it against your even product. You won't be able to use sum or product because you won't know the order of the odd or even numbers for every possible input. (For your example, you do know that every other number is odd or even.)
Start with the above and use a for loop to iterate over the data.
  댓글 수: 7
Joseph Cheng
Joseph Cheng 2015년 6월 3일
편집: Joseph Cheng 2015년 6월 3일
well it wasn't my code Q('-'Q), i reposted Gina's code since I don't have editing privileges yet. Are you looking for just for it being odd then you should be using mod()==1 to check for odd values. if you're checking for odd values and whether it is within the array then
if mod(ii,2)==1 & any(ii==data)
check first to see if it is odd, and does the logical operand AND to say also is that number part of the data array (which it should be since ii = data(1:end).) which is kind of redundant since data = data(1:end)
Gina Barlage
Gina Barlage 2015년 6월 3일
Thanks so much for all your help!

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


Walter Roberson
Walter Roberson 2015년 6월 3일
Hint:
f = @(x) sin(x).^2; %does a calculation and returns value
r = rand(1,20);
condition_met = f(r) < 0.33; %does the calculated value satisfy some condition?
values_true_for = r(condition_met);
values_false_for = r(~condition_met);

카테고리

Help CenterFile Exchange에서 Model References에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by