Does anybody know?

A line of code i could use for my function so when i insert a vector such as [1 2 3 4], the function i am making see's it as the value 1234(one thousand, two hundred and thirty four) instead? Thanks :)

 采纳的回答

str2double(sprintf('%d',V))
or
str2double(char(V+'0'))
which should be more efficient.
Warning: if your vectors might have 15 to 19 elements, then there would be additional work involved in order to get the correct integer. From 19 elements up, only an approximation of the integer can be produced.

9 个评论

So far the code for my function is this:
function [q,r]=Divide(x,y)
q=x/y;
q=fix(q);
r=x-(q*y);
end
So if i just insert either str2double(sprintf('%d',V)) or str2double(char(V+'0')) somewhere into there. It will read vectors for x and y that i submit into the command window of the function,as integers? and also produce the ans as a vector also?
Well you would use x or y instead of V.
x = str2double(char(x+'0'));
y = str2double(char(y+'0'));
It would not create your answer as a vector unless you added more code at the end:
q = sprintf('%d',q) - '0';
r = sprintf('%d',r) - '0';
This is good thanks, but only seems to work for when my x and y vector consist of 4 digits :(.
The function i am trying to build is one which will dividie the x value by the y value. But both x and y must be inserted as a vector (but actually a whole number) and come out as a vector (but the vector represents a whole number).
function [q,r]=Divide(x,y)
q=x/y;
q=fix(q);
r=x-(q*y);
x = str2double(char(x+'0'));
y = str2double(char(y+'0'));
q = sprintf('%d',q) - '0';
r = sprintf('%d',r) - '0';
end
thats my code so far, could you give me some tips please? i really cant get my head around whats going wrong !
Thanks!
The x= and y= have to go as the first things after the "function" line.
Thanks :)
Sorry i only just noticed your warning about the 19 elements up needing additional work, is there much more i would need to put into my program to get it to work for over 19 elements etc?
function [q,r]=Divide(x,y)
for x = str2double(char(x+'0'));
y = str2double(char(y+'0'));
end
assert(0<y<10, 'Y can only only be between the values of 1...9')
q=x/y;
q=floor(q);
r=x-(q*y);
q = sprintf('%d',q) - '0';
r = sprintf('%d',r) - '0';
end
Or am i going to have to basically make a whole new function?
If you need to handle more than 19 elements, then you are going to have to write a whole new function based on very different computation principles. I think it likely that it was intended that you use those computation principles right from the beginning.
Your algorithm will need to implement "long division" or the equivalent.
hoooray -_-

请先登录,再进行评论。

更多回答(1 个)

x = [1 2 3 4];
str2num(num2str(x')')

1 个评论

Sorry i deleted my previous reply, what i meant to say is that this only works for when x and y are the same size vector (x=[2 2 2 2], y=[1 1 1 1]. Sorry for being a pain, but do you know what i could do to my code so that it will work even if x and y do not contain the same amount of digits inside of them. My functions plans on dividing x by y.

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by