to make a scaler from a vector

I have vector and I want to make him a scalat without functions

3 Comments

What do you mean? Give numeric examples.
vec=[2 8 7] to make it a number (287) without functions
What does without functions mean?
Just by writing
vec = [2 8 7]
you've already used the function horzcat.

Sign in to comment.

Answers (2)

a = [1 10 100];
str2num(strrep(num2str(a), ' ', ''))

1 Comment

I avoid the indirection over a different type usually. But here you could use the simpler sprintf:
sscanf(sprintf('%d', a), '%d')

Sign in to comment.

Jan
Jan on 23 Jan 2018
Edited: Jan on 23 Jan 2018
vec = [2 8 7];
num = vec * 10 .^ (length(vec)-1:-1:0).';
In your case this is
[2, 8, 7] * [100; 10; 1]
This works only, if the elements of the inputs are the digits 0 to 9 and if the length of the input is less than 16 digits. Otherwise the output cannot be stored in a double exactly.

Tags

Asked:

on 23 Jan 2018

Commented:

on 23 Jan 2018

Community Treasure Hunt

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

Start Hunting!