how can i convert an integer to an array in this function?

function a=get_array_num(n)
end
requirments:
1.no loops,if,recurion or logical operatos
2.no num2str or str2num
3.length_of(n)=number of digits in "n" (help function)
i think maybe logspace is useful here...but how?

8 件のコメント

Walter Roberson
Walter Roberson 2013 年 3 月 31 日
That does not define any return value "a".
The only way that you can meaningfully have length(n) be the same as the number of digits in n, is if n is a scalar value (length 1) with a single digit.
Zaza
Zaza 2013 年 3 月 31 日
function r = length_of(n)
if n < 10
r = 1 ;
else
r = 1 + length_of(n/10) ;
end
end
Image Analyst
Image Analyst 2013 年 3 月 31 日
編集済み: Image Analyst 2013 年 3 月 31 日
That would mean your "a" (now called "r") is an array of 1 - not really much of an array. Besides - that code uses recursion, which is not allowed.
Zaza
Zaza 2013 年 3 月 31 日
編集済み: Zaza 2013 年 3 月 31 日
"a" is not "r" and vice versa
the function "length_of(n)" is known and can be used in the function "get_array_num(n)"
Zaza
Zaza 2013 年 3 月 31 日
true
i forgot to mention that "length_of(n)" is recursive regardless of "get_array_num(n)"
Walter Roberson
Walter Roberson 2013 年 4 月 1 日
Why is length_of(0) 1 instead of 0? Why is length_of(-23) 1 instead of 3?
Zaza
Zaza 2013 年 4 月 1 日
sorry again
suppose n>0 with no leading zeros
Jan
Jan 2013 年 4 月 2 日
For length_of() the log10 function would be much smarter.

サインインしてコメントする。

回答 (1 件)

Image Analyst
Image Analyst 2013 年 3 月 31 日

0 投票

How about
a = zeros(n);
or
a = n * ones(1, 10);
or something similar? There is an infinite variety of arrays you could make that would full those requirements, though requirement #3 is not well explained at all (no idea what it even means).

16 件のコメント

Walter Roberson
Walter Roberson 2013 年 3 月 31 日
I see nothing in the requirements that the output must have any relationship to the input.
Image Analyst
Image Analyst 2013 年 3 月 31 日
True, so there are even more infinite ways to answer. This is so poorly described, even with additional comments, that I don't even know how to help anymore.
Zaza
Zaza 2013 年 4 月 1 日
編集済み: Zaza 2013 年 4 月 1 日
there are no infinite ways to answer and i'm sorry if it's described poorly.
the help function is given and that's it. of course the main function must meet the conditions.
Walter Roberson
Walter Roberson 2013 年 4 月 1 日
You have not indicated what the result of the function is to be.
Zaza
Zaza 2013 年 4 月 1 日
for example:
n=8427
a=[8 4 2 7]
Image Analyst
Image Analyst 2013 年 4 月 1 日
strN = num2str(n) % Convert to string.
a = strN - '0' % Convert characters to separate numbers.
Walter Roberson
Walter Roberson 2013 年 4 月 1 日
num2str() was prohibited in the assignment.
Image Analyst
Image Analyst 2013 年 4 月 1 日
What a stupid requirement. Then use sprintf() as the "loophole":
strN = sprintf('%d',n)
a = strN - '0'
Zaza
Zaza 2013 年 4 月 1 日
編集済み: Zaza 2013 年 4 月 1 日
i read the assigmnet again and it said no use of any functions that convert from numbers to strings and vice versa so i guess it's not legal to use _sprintf() _but thanks anyway
Walter Roberson
Walter Roberson 2013 年 4 月 1 日
Perhaps you should post the exact assignment.
Image Analyst
Image Analyst 2013 年 4 月 1 日
I did it in 5 lines with a for loop, rem(), floor(), and finally fliplr() to reverse the last digits.
  1. for
  2. call rem
  3. reassign n
  4. end
  5. call fliplr
I'm sure you don't want me to just give you the answer outright (because you could be considered as not doing your own work) so give that a try.
Walter Roberson
Walter Roberson 2013 年 4 月 1 日
The requirements include 'no loops'
Image Analyst
Image Analyst 2013 年 4 月 1 日
Ha! I give up. Who wants to work like that? I hate it when they say to use MATLAB but don't let you use any of the power of it. I don't see the point. I don't see any way, off the top of my head, to march along the digits of your integer if you can't use strings, loops of any type, or recursion if your integer is of unknown length. Of course if you know it's always 4 digits, then you can just write the contents of the for loop 4 times. Good luck with it though. If something strikes me, I'll post a hint.
Walter Roberson
Walter Roberson 2013 年 4 月 1 日
I can do it in one smallish line, once given the length_of function. Loops may have been prohibited but ":" has not been.
Zaza
Zaza 2013 年 4 月 2 日
may i have a clue how to do it with the colon operator?
Jan
Jan 2013 年 4 月 2 日
@Zaza: It consumed some time and energy to bring you to post an explicit example. We get some parts of the problem distributed to the question and over several comments. Therefore an efficient answer is impossible.
Please edit the original question and insert all available information. Then show us, what you have tried so far, because of course we are not going to solve your homework: you couldn't submit it anymore without cheating!

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

タグ

質問済み:

2013 年 3 月 31 日

Community Treasure Hunt

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

Start Hunting!

Translated by