Borrar filtros
Borrar filtros

transform the number cell so that only numbers (with or without decimals) excluding zeros are visible

5 visualizaciones (últimos 30 días)
Hi. I want to transform the number cell "N" so that only numbers (with or without decimals) excluding zeros are visible (see for example the number 100 circled in green). Shown are the "transformations" I would like to apply to all numbers inside cell "N". I have tried a for loop but that does not think to be the right way.
N = importdata("N.mat");
% N1 = cell2table(N);
N_new = {};
for K = 1:height(N)
for k = 1:width(N)
aa = N{K,k};
F = compose('%.2f',aa);
end
N_new = [N_new,{F}];
end
  2 comentarios
Matt J
Matt J el 15 de Sept. de 2023
see for example the number 100 circled in green
This hints that your post should contain an image for us to look at, but I see none.
Stephen23
Stephen23 el 15 de Sept. de 2023
Why are you very inefficiently storing lots of scalar numerics in a cell array?
"I have tried a for loop but that does not think to be the right way."
You are forced to write a loop because you are not storing your data the right way (in a numeric matrix).

Iniciar sesión para comentar.

Respuesta aceptada

Matt J
Matt J el 15 de Sept. de 2023
load N
string(N)
ans = 8×8 string array
"100" "100" "100" "100" "100" "100" "100" "100" "100.01" "100.01" "100.01" "99.99" "100.01" "100.01" "100.01" "99.98" "42.35" "47" "46.84" "45.65" "47.01" "47.01" "46.85" "45.65" "57.65" "53" "53.16" "54.35" "52.99" "52.99" "53.15" "54.35" "100" "100" "100" "100" "100" "100" "100" "100" "3.7" "4.35" "3.38" "3.59" "4.34" "4.34" "3.37" "3.57" "53.95" "48.65" "49.78" "50.76" "48.65" "48.65" "49.78" "50.78" "57.65" "53" "53.16" "54.35" "52.99" "52.99" "53.15" "54.35"
  3 comentarios
Matt J
Matt J el 15 de Sept. de 2023
That would be a peculiar thing to do, because strings already have cell-like indexing syntax,
load N
Ns=string(N);
Ns{2,1}
ans = '100.01'
However, if you're sure it's what you want,
Nscell=num2cell(string(N))
Nscell = 8×8 cell array
{["100" ]} {["100" ]} {["100" ]} {["100" ]} {["100" ]} {["100" ]} {["100" ]} {["100" ]} {["100.01"]} {["100.01"]} {["100.01"]} {["99.99"]} {["100.01"]} {["100.01"]} {["100.01"]} {["99.98"]} {["42.35" ]} {["47" ]} {["46.84" ]} {["45.65"]} {["47.01" ]} {["47.01" ]} {["46.85" ]} {["45.65"]} {["57.65" ]} {["53" ]} {["53.16" ]} {["54.35"]} {["52.99" ]} {["52.99" ]} {["53.15" ]} {["54.35"]} {["100" ]} {["100" ]} {["100" ]} {["100" ]} {["100" ]} {["100" ]} {["100" ]} {["100" ]} {["3.7" ]} {["4.35" ]} {["3.38" ]} {["3.59" ]} {["4.34" ]} {["4.34" ]} {["3.37" ]} {["3.57" ]} {["53.95" ]} {["48.65" ]} {["49.78" ]} {["50.76"]} {["48.65" ]} {["48.65" ]} {["49.78" ]} {["50.78"]} {["57.65" ]} {["53" ]} {["53.16" ]} {["54.35"]} {["52.99" ]} {["52.99" ]} {["53.15" ]} {["54.35"]}
Stephen23
Stephen23 el 15 de Sept. de 2023
It is not recommended to use cell arrays of (scalar) string arrays, because these are very ineffiicient and do not allow any of the benefits of using string arrays:
However it would certainly be okay to have a cell array of character vectors (e.g. using COMPOSE or CELLSTR).

Iniciar sesión para comentar.

Más respuestas (2)

Matt J
Matt J el 15 de Sept. de 2023
If your're just looking for a way to change the display format, and not the numbers themselves, I think you can only do that in the Command Window, not the Variables Editor.
load N
format shortG
disp(N)
{[ 100]} {[ 100]} {[ 100]} {[ 100]} {[ 100]} {[ 100]} {[ 100]} {[ 100]} {[100.01]} {[100.01]} {[100.01]} {[99.99]} {[100.01]} {[100.01]} {[100.01]} {[99.98]} {[ 42.35]} {[ 47]} {[ 46.84]} {[45.65]} {[ 47.01]} {[ 47.01]} {[ 46.85]} {[45.65]} {[ 57.65]} {[ 53]} {[ 53.16]} {[54.35]} {[ 52.99]} {[ 52.99]} {[ 53.15]} {[54.35]} {[ 100]} {[ 100]} {[ 100]} {[ 100]} {[ 100]} {[ 100]} {[ 100]} {[ 100]} {[ 3.7]} {[ 4.35]} {[ 3.38]} {[ 3.59]} {[ 4.34]} {[ 4.34]} {[ 3.37]} {[ 3.57]} {[ 53.95]} {[ 48.65]} {[ 49.78]} {[50.76]} {[ 48.65]} {[ 48.65]} {[ 49.78]} {[50.78]} {[ 57.65]} {[ 53]} {[ 53.16]} {[54.35]} {[ 52.99]} {[ 52.99]} {[ 53.15]} {[54.35]}
  1 comentario
Alberto Acri
Alberto Acri el 15 de Sept. de 2023
Thanks for your answer.
I want to create a new cell with numbers like the number 100 circled in green (obviously it depends on whether that number has one or two numbers after the decimal point).

Iniciar sesión para comentar.


Matt J
Matt J el 15 de Sept. de 2023
Editada: Matt J el 15 de Sept. de 2023
You can round all of the numbers to 2 decimal places as below. This will make it so that any number that was an integer to 2 decimal places will be displayed as an integer without any trailing zeros. However, there is no way to get numeric non-integers to display with less than 4 decimal places in the Variables Editor, and not in the Command Window either, except with format shortG or format bank.
load N
N, %original
N = 8×8 cell array
{[ 100]} {[100.0000]} {[ 100]} {[ 100]} {[ 100]} {[ 100]} {[100.0000]} {[100.0000]} {[100.0100]} {[100.0100]} {[100.0100]} {[99.9900]} {[100.0100]} {[100.0100]} {[100.0100]} {[ 99.9800]} {[ 42.3500]} {[ 47]} {[ 46.8400]} {[45.6500]} {[ 47.0100]} {[ 47.0100]} {[ 46.8500]} {[ 45.6500]} {[ 57.6500]} {[ 53.0000]} {[ 53.1600]} {[54.3500]} {[ 52.9900]} {[ 52.9900]} {[ 53.1500]} {[ 54.3500]} {[100.0000]} {[100.0000]} {[100.0000]} {[ 100]} {[100.0000]} {[100.0000]} {[100.0000]} {[100.0000]} {[ 3.7000]} {[ 4.3500]} {[ 3.3800]} {[ 3.5900]} {[ 4.3400]} {[ 4.3400]} {[ 3.3700]} {[ 3.5700]} {[ 53.9500]} {[ 48.6500]} {[ 49.7800]} {[50.7600]} {[ 48.6500]} {[ 48.6500]} {[ 49.7800]} {[ 50.7800]} {[ 57.6500]} {[ 53.0000]} {[ 53.1600]} {[54.3500]} {[ 52.9900]} {[ 52.9900]} {[ 53.1500]} {[ 54.3500]}
N=cellfun(@(z)round(z,2), N, 'uni',0) %rounded
N = 8×8 cell array
{[ 100]} {[ 100]} {[ 100]} {[ 100]} {[ 100]} {[ 100]} {[ 100]} {[ 100]} {[100.0100]} {[100.0100]} {[100.0100]} {[99.9900]} {[100.0100]} {[100.0100]} {[100.0100]} {[99.9800]} {[ 42.3500]} {[ 47]} {[ 46.8400]} {[45.6500]} {[ 47.0100]} {[ 47.0100]} {[ 46.8500]} {[45.6500]} {[ 57.6500]} {[ 53]} {[ 53.1600]} {[54.3500]} {[ 52.9900]} {[ 52.9900]} {[ 53.1500]} {[54.3500]} {[ 100]} {[ 100]} {[ 100]} {[ 100]} {[ 100]} {[ 100]} {[ 100]} {[ 100]} {[ 3.7000]} {[ 4.3500]} {[ 3.3800]} {[ 3.5900]} {[ 4.3400]} {[ 4.3400]} {[ 3.3700]} {[ 3.5700]} {[ 53.9500]} {[ 48.6500]} {[ 49.7800]} {[50.7600]} {[ 48.6500]} {[ 48.6500]} {[ 49.7800]} {[50.7800]} {[ 57.6500]} {[ 53]} {[ 53.1600]} {[54.3500]} {[ 52.9900]} {[ 52.9900]} {[ 53.1500]} {[54.3500]}

Categorías

Más información sobre Logical en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by