How to add or multiply digits of a number [closed] - matlab

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
As the title says i want to know if its possible to add or multiply digits of a number in matlab
eg:
for a number
123456789
add the digits, that is
1+2+3+4+5+6+7+8+9 =>45 =>4+5 => 9
advance thanks for your help

Numeric approach
A = 35356536576821;
A = abs(A);
xp = ceil(log10(A)):-1:1;
while ~isscalar(xp)
A = sum(fix(mod(A,10.^xp)./10.^[xp(2:end) 0]));
xp = ceil(log10(A)):-1:1;
end
Char approach
A = '35356536576821';
A = char(regexp(A,'\d+','match'));
while ~isscalar(A)
A = num2str(sum(A - '0'));
end
Both, first take the absolute number (strip the minus) then: the numeric one counts with log10() how many digits a number has and through modulus and divisions extracts the digits which are summed, while the char approach convert to numeric digits with implicit conversion of - '0', sums and converts back to string again.
Both approaches might suffer from floating point approximation, but the numeric one is definitely more exposed to it:
A = 11111111111111111;
xp = ceil(log10(A)):-1:1;
fix(mod(A,10.^xp)./10.^[xp(2:end) 0])
ans =
Columns 1 through 13
1 1 1 1 1 1 1 1 1 1 1 1 1
Columns 14 through 17
1 1 1 2
To have approximations with the char approach, the first sum should exceed a 16 digits number.

Related

Permutation of a vector and its sub elements using MATLAB [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have the vector below:
v={'T','AT','AS','C'};
I would like to see all the possible permutations for this vector. To do so I can use the command below:
p=perms(v)
But I want to go one step further as each of the elements has sub index of 1 to 4, for example, T1,T2,T3,T4 .....C1,C2,C3,C4. I would like to have all the possible permutations with its sub index as see such results
T1,AT1,AS1,C1
C3,AT3,AS3,t3
AS2,AT2,C2,T2
.
.
.
Could you please help me how to do that?
Thanks
You can do this by first using ndgrid to generate a set of indices for all your possible combinations:
v = {'T1', 'AT1', 'AS1', 'C1'; ...
'T2', 'AT2', 'AS2', 'C2'; ...
'T3', 'AT3', 'AS3', 'C3'; ...
'T4', 'AT4', 'AS4', 'C4'};
[ind1, ind2, ind3, ind4] = ndgrid(1:4);
c = [v(ind1(:), 1) v(ind2(:), 2) v(ind3(:), 3) v(ind4(:), 4)];
And c will be a 256-by-4 cell array, as expected (44 combinations). Now you can expand each row by it's total number of permutations using perms like so:
p = perms(1:4);
p = reshape(c(:, p.').', 4, []).';
And p will be a 6144-by-4 cell array, also as expected (24 permutations times 256 combinations).

using for loop for fibonacci series to print values it will print up to 47 values above that it shows error [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
var num = "100"
var num = text2.text.toInt()
var temp = 0
var temp2 = 1
if (nu == 1) {
println(1)
}
else {
for var valued = 2; valued<num;++valued {
var temp3 = temp + temp2
temp = temp2
temp2 = temp3
println("\(temp3)")
}
I want to print the fibonacci series of number. The number should be any one that should be choose by user. My code is above i have to choose num as 100 but it will print up to 47 values. The final value print here is 1836311903. It will not print up to 100. It shows an error. How i find the fibonacci series for number 100.
Fib(47) is 2,971,215,073.
2,971,215,073 is larger than 231 - 1 ... which is the largest 32 bit signed integer.
Hence your calculation is overflowing. (In a lot of programming languages, you wouldn't have gotten an error. You would have just gotten the wrong answer.)
How i find the fibonacci series for number 100.
You can't use simple integer arithmetic. You will need to use the Swift equivalent of Java's BigInteger class.
BigInteger equivalent in Swift?

Matlab- Max number of digits after decimal point [duplicate]

This question already has answers here:
How to have sprintf to ignore trailing zeros
(2 answers)
Closed 8 years ago.
I would like to have max 3 digits after decimal point when it is necessary.Sprintf and format bank do not give me what I need. This numbers are going to be in a text box on a figure.
Basically what I tried :
tt=2.4242
sprintf('%.3f', tt)
tt=2.424
that's good for the numbers that have 3 or more digits after decimal point but what if I have no digit ( For the math guys : I mean 0 after decimal point) or 1 digit, it doesn't look that good. For example:
tt= 0
sprintf('%.3f', tt)
tt=0.000
Is there a function for that or do I have to do that with if or for?
I appreciate your help!
After sprintf('%.3f', tt), use regexprep to
remove trailing zeros, if any;
remove also the decimal point if all digits after it are zero.
That is:
regexprep(sprintf('%.3f', tt), '(\.*0+)$', '')
Examples:
>> tt = 4.1; regexprep(sprintf('%.3f', tt), '(\.*0+)$', '')
ans =
4.1
>> tt = 4; regexprep(sprintf('%.3f', tt), '(\.*0+)$', '')
ans =
4
Try g format specifier instead of f:
sprintf('%.4g', tt)
See also: How to have sprintf to ignore trailing zeros (Give #RTL is due, I asked the same question a few days ago, this is why I know the answer).

How many have a name longer than 10 characters in xlsx matlab [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
How many elements have a name longer than 10 characters in xlsx matlab?
I tried this.
[number cities] = xlsread('weather.xlsx', 'city')
if cities{1}<=char(10);
x+1=x;
Your code above only checks to see if the first city has less than 10 characters. Also, char(10) does not make any sense. You are checking to see if the string contained in cities{1} is less than or equal to the character array containing 10.
Because cities is in a cell array, I would use cellfun to first return the length of each city. Then you can use a sum as well as a Boolean condition to help you figure out how many there are that have more than 10 characters.
As such, here is the code:
A = cellfun(#length, cities);
numCitiesMoreThan10 = sum(A > 10);
Here is an example (simulated):
cities = {'New York', 'Los Angeles', 'Toronto', 'Ottawa', 'Sydney', 'Melbourne', ...
'Timbuktu', 'Singapore', 'Mississippi'};
A = cellfun(#length, cities);
numCitiesMoreThan10 = sum(A > 10);
>> numCitiesMoreThan10 =
2
This makes sense, as the only two cities with more than 10 characters (including spaces) are Los Angeles and Mississippi.
Aside
Just learned that cellfun has something built-in that can do this. You can also do:
A = cellfun('size', cities, 2);
This accesses each element in the cell array (cities), and returns the size of whatever dimension you specify in the last parameter of cellfun when you call it with the size parameter. You can also chain more than one cell array together. The reason why you are choosing the third parameter as 2 is because each string in the cell array is a 1 x N array. As such, we need to read how many columns there are so that this equates to the length of each string.

Matlab, how to convert a string of integers into a vector? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
convert string to number array in matlab
Is there a simple way in Matlab to convert a string like this
'123456789'
into a vector like this ?
[1 2 3 4 5 6 7 8 9]
If all you have is contiguous characters from 0 to 9:
v = double(s)-'0';
double(s) converts a string into an array where each element is the ASCII code of the corresponding character. To obtain the numberic values we subtract '0' (which is in fact 48 in ASCII) and since digits have a sequential representation in ASCII code ('1' = 49, '2' = 50, etc.) we end up with intended result.
one way would be using regexp for this. But of course it only works for single digit numbers.
>> str = '123456789';
>> num = regexp(str,'\d')
num =
1 2 3 4 5 6 7 8 9