MATLAB Answers

Trying to make a function for summing up 3 numbers. Explaination below

4 ビュー (過去 30 日間)
Konstantinos Kamperos
Konstantinos Kamperos 2015 年 3 月 29 日
編集済み: Stephen Cobeldick 2015 年 3 月 30 日
Hello, I am trying to make a function that can split any type of number ( I am using Getdigits for that ) and sum the digits. I want the user to be able to input any digit number in a type of [x y z] and then be able to find if any of the numbers are odd. Unfortunately I am new to Matlab and I am not really able to make it work. Can anyone help me please. Thank you

  0 件のコメント

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

採用された回答

Youssef  Khmou
Youssef Khmou 2015 年 3 月 29 日
the following answer is an alternative approach without using 'getdigits' function :
function [C,y]=sum3(x)
h=int2str(x);
N=length(h);
for n=1:N
C(n)=str2num(h(n));
end
y=sum(C);

  1 件のコメント

Konstantinos Kamperos
Konstantinos Kamperos 2015 年 3 月 29 日
Thank you a lot. This makes things way easier :). Is there any way to make this work for 3 numbers in a format of [x y z] without typing the code 3 times?

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

その他の回答 (2 件)

Stephen Cobeldick
Stephen Cobeldick 2015 年 3 月 29 日
編集済み: Stephen Cobeldick 2015 年 3 月 30 日
Although an answer has already been accepted, that answer shows very poor use of MATLAB by solving this using nested loops and multiple calls to the rather slow str2num within each loop iteration. A much simpler, neater and faster solution would be to vectorize the whole code and use basic MATLAB practices, something like this:
>> A = [123,4,567890];
>> B = int2str(A(:))-48;
>> B(B<0) = 0
B =
0 0 0 1 2 3
0 0 0 0 0 4
5 6 7 8 9 0
>> sum(B,2)
ans =
6
4
35
This will work for vectors of any length (i.e. A can have any number of values in it), it will be faster that a loop-based solution (especially if A has more values in it), and it uses fewer lines of code too!

  2 件のコメント

Konstantinos Kamperos
Konstantinos Kamperos 2015 年 3 月 29 日
Thank you a lot. It seems amazing. I do not yet understand how this code can work that well and be so small but I will research it. I appreciate your answer :)
Stephen Cobeldick
Stephen Cobeldick 2015 年 3 月 30 日
MATLAB is a high-level language, which means that it has faster and neater ways of solving basic arithmetic than using loops: loops are the second choice in MATLAB, code vectorization is the preferred choice! Loops are what are needed in low-level languages like C, etc.
The concept is very simple: many basic arithmetic operations and MATLAB functions operate on complete matrices at once, without requiring any loops. This is called vectorization, and it is much faster, neater code, and it looks more like the mathematics as it would be written.
If you want to learn MATLAB, then do not learn bad programming habits first: instead learn the recommended practices and your code will be much improved.

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


Youssef  Khmou
Youssef Khmou 2015 年 3 月 30 日
編集済み: Youssef Khmou 2015 年 3 月 30 日
This function works for a vector of length n, with second logical output according to the description :
function [Logical,y]=sumn(x)
N=length(x);
for iter=1:N
h=int2str(x(iter));
NN=length(h);
C=zeros(1,NN);
for n=1:NN
C(n)=str2num(h(n));
end
S=sum(C);
y(iter)=S;
if mod(S,2)==0
Logical(iter)=true;
else
Logical(iter)=false;
end
end

  0 件のコメント

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

Community Treasure Hunt

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

Start Hunting!

Translated by