Add number to every value in the vectors within a cell array (without loop)

23 visualizaciones (últimos 30 días)
Hello all,
This is probably a simple question.
I would like to be able to add a constant to every value in the vectors within a cell array without using a loop.
Here is my current code. Which works, but doesnt seem very elegant:
T1 = 1:1:10;
T2 = 11:1:20;
T3 = 21:1:30;
Temp = [{T1};{T2};{T3}];
for i = 1:20
Temp(i) = {bsxfun(@plus, TempC{i, 1}, 273.15)};
end

Respuesta aceptada

DGM
DGM el 10 de Feb. de 2022
Editada: DGM el 10 de Feb. de 2022
You can use cellfun() to apply an operation to the contents of a cell array. Elementwise arithmetic operations between a scalar and an array don't need bsxfun() to do the expansion, even in versions prior to R2016b.
T1 = 1:1:10;
T2 = 11:1:20;
T3 = 21:1:30;
Temp = [{T1};{T2};{T3}];
Temp = cellfun(@(x) x+273.15,Temp,'uniform',false);
celldisp(Temp)
Temp{1} = 274.1500 275.1500 276.1500 277.1500 278.1500 279.1500 280.1500 281.1500 282.1500 283.1500 Temp{2} = 284.1500 285.1500 286.1500 287.1500 288.1500 289.1500 290.1500 291.1500 292.1500 293.1500 Temp{3} = 294.1500 295.1500 296.1500 297.1500 298.1500 299.1500 300.1500 301.1500 302.1500 303.1500

Más respuestas (0)

Categorías

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

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