Add number to every value in the vectors within a cell array (without loop)
31 次查看(过去 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
0 个评论
采纳的回答
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)
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!