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