delete all the decimal digits that are 0 after the first decimal place

Hi! I need to transform the vector V with only the first decimal. Is there an easy way to do this?
For example I am trying this way but the 9 becomes 9.0:
V = [
6.20000000000000
7.50000000000000
9
10.2000000000000
9.40000000000000];
F = {};
for ii = 1:height(V)
FF = sprintf('%.1f', V(ii));
F = [F,{FF}];
end

1 Comment

Do you want the output to be numeric or string/char?
It would be better if you could specify what the expect output is.

Sign in to comment.

 Accepted Answer

Using %g instead of %.1f does what you want, for this example at least.

Also, you can use compose instead of the for loop:

F = compose('%g',V);

7 Comments

To turn the cell into double how come it doesn't fit like this?
F = cell2mat(F);
Because the cell arrays contains elements with inconsistent size for vertical concatenation.
V = [6.20000000000000
7.50000000000000
9
10.2000000000000
9.40000000000000];
F = compose('%g',V)
F = 5×1 cell array
{'6.2' } {'7.5' } {'9' } {'10.2'} {'9.4' }
cellfun('length', F)
ans = 5×1
3 3 1 4 3
Once again, Do you want the final output to be numeric or string/char?
I want to achieve this result (5x1 double):
V_out = [6.2
7.5
9
10.2
9.4];
@Alberto Acri The values 6.2, 10.2, and 9.4 cannot be represented exactly in IEEE double precision floating point. You can round the numbers to 1 decimal place, but the result won't contain exactly the numbers you list above ... they will be the closest approximations representable in IEEE double. Is that good enough for you? E.g.,
format longg
x = 6.2000001 % something close
x =
6.2000001
x = round(x,1) % round it
x =
6.2
fprintf('%55.50f\n',x) % display what is actually stored
6.20000000000000017763568394002504646778106689453125
"I want to achieve this result (5x1 double)"
Numeric data types do not store any formatting information. How many trailing digits are displayed is purely an artifact of the display routine and makes absolutely no difference to the numeric data stored in memory.
So far you have not explained what you intend to do with this numeric data. So far we have this:
So the cell created like this:
F = compose('%g',V)
can't be transformed into a double vector?
% F (5x1 cell) --> F (5x1 double)
You are confusing how numbers are stored with how they are displayed .
6.2 exactly cannot be stored in any basic MATLAB numeric format.
MATLAB uses finite representation of numbers, and it is a mathematical truth that for any finite fixed-point representation or finite floating-point representation in any mathematical base (such as decimal -> base 10, binary -> base 2) that there are numbers that cannot be exactly represented. Base 10 (decimal) has this problem to. Suppose that you are using 10-digit numbers in base 10, and represent 1/3 -> 0.3333333333 . Now add 3 of them and you get 0.999999999 even though 3 * (1/3) should be exactly 1. So there are a lot of numbers that finite base 10 cannot represent exactly. MATLAB uses base 2, and has a different set of numbers that cannot be represented exactly, but this is not a "bug", it is a fundamental mathematical limitation of finite representation.
Rather than display the full complete decimal equivalent of each number, MATLAB's display routines show an approximation of the number. The level of detail of the approximation depend upon what setting of format you are using.
If you are using format short g then MATLAB displays up to 5 significant figures . After it has internally rounded to 5 significant figures, it removes trailing 0s from fractions:
format short g
V = [
6.20000000000000
7.50000000000000
9
10.2000000000000
9.40000000000000]
V = 5×1
6.2 7.5 9 10.2 9.4
This is for display and does not affect how the value is stored. For example the third entry might display as 9 with no decimal place, but it will still be stored as a double precision number.
You can use round(V,1) to round the elements in V to the nearest representable number to rounding to one decimal place. That would affect what was stored -- but not (directly) what was displayed.

Sign in to comment.

More Answers (1)

Here is one slightly different option:
V = [
6.20000000000000
7.50000000000000
9
10.2000000000000
9.40000000000000];
for ii=1:numel(V)
FF{ii} = num2str(round(V(ii), 1), '%.1f');
end
FF
FF = 1×5 cell array
{'6.2'} {'7.5'} {'9.0'} {'10.2'} {'9.4'}

Products

Release

R2021b

Community Treasure Hunt

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

Start Hunting!