Optimization using Cell Arrays
4 ビュー (過去 30 日間)
古いコメントを表示
Hello,
I'v been trying to optimize the existing code below. This calculation is done one at a time for 336000 of c1, r1, and slope.
Rinter = zeros(1080,1);
for r = 1:1080
Rinter(r) = c1 + (r-r1) / slope;
end
I redid the variables c1,r1,slope to be calculated all at once using cellfun. So now I have r1,c1,slope = 336 cell arrays with each cell holds 1000 values. I'm trying to figure out how to optimize the code so that:
Rinter = zeros(1080,1);
for r = 1:1080
Rinter(r) = c1{1:336,1}(1:1000) + (r-r1{1:336,1}(1:1000)) / slope{1:336}(1:1000);
end
any help is appreciated since I'm rather confused now. Thanks!
5 件のコメント
Torsten
2023 年 7 月 12 日
編集済み: Torsten
2023 年 7 月 12 日
I don't understand your example. Since r is a scalar in your equation
Rinter(r) = c1{1:336,1}(1:1000) + (r-r1{1:336,1}(1:1000)) / slope{1:336}(1:1000);
shouldn't the components of the vector R = r*[1; 1; 1] be the same ?
r1{1,1} = ...
[538.066700563257
539.176467549361
537.641860711957];
c1{1,1} = ...
[145.906518570733
146.998492549802
145.488490220006];
slope{1,1} = ...
[-0.980866192253173
-0.989057988958667
-0.982385707846712];
Rinter{1,1} = ...
[693.449807150745
692.430300098817
691.410793046889];
R = (Rinter{1,1}-c1{1,1}).*slope{1,1}+r1{1,1}
回答 (1 件)
Image Analyst
2023 年 7 月 12 日
Just use a normal double array. Cell arrays are slow and very inefficient and are a special type of variable for situations where every element (cell) might have a different type or different number of elements in each cell. See the FAQ:
Since that does not apply in your situation, a normal double array would be the best, fastest, and most efficient approach.
3 件のコメント
Rik
2023 年 7 月 13 日
You should use timeit when dealing with code that takes less than a second. You should also properly preallocate the output arrays.
r2l = zeros(1,336);
c2l = zeros(1,336);
for i = 1:336
[r2l(i),c2l(i)] = RPD(r2(i),c2(i),angle);
end
参考
カテゴリ
Help Center および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!