How to combine multiple numbers into 1 number?

I have the numbers R1, R2, Zr1, that I want to fuse together into 24000 . Zr1 is an array, while R1 and R2 are double. I have tried strcat but it only produces 240 0 0 which is not the intended 24000
R1 = 2
R2 = 4
Zr1 = 0 0 0

1 Comment

"Zr1 is an array, while R1 and R2 are double"
All doubles are arrays. Scalars are not a separate type, because in MATLAB everything is an array:

Sign in to comment.

 Accepted Answer

General solution:
R1 = 2;
R2 = 4;
Zr1 = [0,0,0];
V = [R1,R2,Zr1];
N = 10.^(numel(V)-1:-1:0) * V(:)
N = 24000

4 Comments

Thanks Stephen! If you don't mind, can you explain how N = 10.^(numel(V)-1:-1:0) * V(:) works?
Since we're doing this numerically, another way to perform that last line is to think of V as a polynomial and evaluate that polynomial at x = 10.
R1 = 2;
R2 = 4;
Zr1 = [0,0,0];
V = [R1,R2,Zr1]
V = 1×5
2 4 0 0 0
N = 10.^(numel(V)-1:-1:0) * V(:)
N = 24000
N2 = polyval(V, 10)
N2 = 24000
syms x
polynomial = x.^(numel(V)-1:-1:0)*V(:)
polynomial = 
N3 = subs(polynomial, x, 10)
N3 = 
24000
N4 = V(1)*1e4 + V(2)*1e3 + V(3)*1e2 + V(4)*1e1 + V(5)*1e0
N4 = 24000
N5 = V(1)*10^4 + V(2)*10^3 + V(3)*10^2 + V(4)*10^1 + V(5)*10^0
N5 = 24000
"can you explain how N = 10.^(numel(V)-1:-1:0) * V(:) works?"
Break code down to know how it works. You can do this yourself.
R1 = 2;
R2 = 4;
Zr1 = [0,0,0];
V = [R1,R2,Zr1] % so V is a 1x5 vector: [10000's,1000's,100's,10's,1's]
V = 1×5
2 4 0 0 0
Clearly all we need to do is to multiply the elements of V by the corresponding power of ten, and then add them together. Note that [10000,1000,100,10,1] is just [1e4,1e3,1e2,1e1,1e0] which is just 10.^[4,3,2,1,0]. So that is very easy to generate:
P = numel(V)-1:-1:0
P = 1×5
4 3 2 1 0
Q = 10.^P
Q = 1×5
10000 1000 100 10 1
Then one simple matrix multiplication does both the multiplying and the addition:
Q * V(:)
ans = 24000
Another way to generate the Q vector is with logspace.
Q = logspace(4, 0, 5)
Q = 1×5
10000 1000 100 10 1

Sign in to comment.

More Answers (1)

Do you mean
R1 = 2; R2 = 4; Zr1 = [0 0 0];
R = R1*10^4+R2*10^3+Zr1(1)*10^2+Zr1(2)*10+Zr1(3)
R = 24000
Alternatively:
R1 = 2; R2 = 4; Zr1 = [0 0 0];
R = [R1, R2, Zr1]
R = 1×5
2 4 0 0 0
M = 10.^[4 3 2 1 0]
M = 1×5
10000 1000 100 10 1
R = M*R'
R = 24000

Categories

Products

Release

R2022b

Asked:

on 25 Feb 2023

Commented:

on 25 Feb 2023

Community Treasure Hunt

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

Start Hunting!