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

23 Ansichten (letzte 30 Tage)
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

Akzeptierte Antwort

DGM
DGM am 10 Feb. 2022
Bearbeitet: DGM am 10 Feb. 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

Weitere Antworten (0)

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Produkte


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by