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

23 次查看(过去 30 天)
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

采纳的回答

DGM
DGM 2022-2-10
编辑:DGM 2022-2-10
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by