A more efficient way to do this calculation

82 次查看(过去 30 天)
Hi all,
I am creating a function that allows me to obtain certain data of Steel Members.
I have created limits that give an interval over which to select a particular member.
Here is my code:
function [HD_cols] = members(Ic);
HD400_1299 = [755000/100^4; 1655/100^2; 1299];
HD400_1202 = [664000/100^4; 1530/100^2; 1202];
HD400_1086 = [596000/100^4; 1386/100^2; 1086];
HD400_990 = [519000/100^4; 1262/100^2; 990];
HD400_900 = [450000/100^4; 1149/100^2; 900];
HD400_818 = [392000/100^4; 1043/100^2; 818];
HD400_744 = [342000/100^4; 948/100^2; 744];
HD400_677 = [300000/100^4; 863/100^2; 677];
HD400_634 = [274000/100^4; 808/100^2; 634];
HD400_592 = [250000/100^4; 755/100^2; 592];
HD400_551 = [226000/100^4; 701/100^2; 551];
HD400_509 = [204000/100^4; 649/100^2; 509];
HD400_463 = [180000/100^4; 590/100^2; 463];
HD400_421 = [160000/100^4; 537/100^2; 421];
T = table(HD400_1299,HD400_1202,HD400_1086,HD400_990,HD400_900,HD400_818,HD400_744,HD400_677,HD400_634,HD400_592,HD400_551,HD400_509,HD400_463,HD400_421);
for i = 1:length(Tx)-1
Limits(i) = (Tx(1,i)+Tx(1,i+1))/2;
end
if Ic > Limits(1)
Ic = HD400_1299
elseif Ic > Limits(2)
Ic = HD400_1202
elseif Ic > Limits(3)
Ic = HD400_1086
elseif Ic > Limits(4)
Ic = HD400_990
elseif Ic > Limits(5)
Ic = HD400_900
elseif Ic > Limits(6)
Ic = HD400_818
elseif Ic > Limits(7)
Ic = HD400_744
elseif Ic > Limits(8)
Ic = HD400_677
elseif Ic > Limits(9)
Ic = HD400_634
elseif Ic > Limits(10)
Ic = HD400_592
elseif Ic > Limits(11)
Ic = HD400_551
elseif Ic > Limits(12)
Ic = HD400_509
elseif Ic > Limits(13)
Ic = HD400_463
elseif Ic > Limits(14)
Ic = HD400_421
end
end
My request is, can somone show me how I can simplify and make more efficient my if statements? Perhaps I need to use a loop? I want to add more members to more script and typing like I have above is tedious and not very efficient. So, I'm hoping someone can help?
Many thanks,
Scott
  3 个评论
Scott Banks
Scott Banks 2025-9-4,12:40
Sorry, i have realised that one vector is missing
Tx = table2array(T);

请先登录,再进行评论。

采纳的回答

Stephen23
Stephen23 2025-9-4,12:52
编辑:Stephen23 2025-9-4,13:42
"A more efficient way to do this calculation"
Use vectors and matrices!
The name MATLAB comes from "MATrix LABoratory", its primary data type is the matrix, because MATLAB processess data efficiently in matrices. When you design your data stored in lots and lots and lots of separate variables then you are shooting yourself in the foot. Placing meta-data in variable names is a very strong sign that you are doing something wrong (and invariably leads users to writing much more complex, inefficient code). Meta-data is data, it should be stored in variables not in variable names.
Here a DIY approach using indexing:
function HD_cols = members1(Ic)
loadVals = [160000; 180000; 204000; 226000; 250000; 274000; 300000; 342000; 392000; 450000; 519000; 596000; 664000; 755000] ./ 100^4;
constVals = [ 537; 590; 649; 701; 755; 808; 863; 948; 1043; 1149; 1262; 1386; 1530; 1655] ./ 100^2;
idVals = [ 421; 463; 509; 551; 592; 634; 677; 744; 818; 900; 990; 1086; 1202; 1299];
midpoints = (loadVals(1:end-1) + loadVals(2:end)) / 2;
midpoints(end+1) = Inf;
idx = find(Ic<=midpoints,1,'first');
HD_cols = [loadVals(idx); constVals(idx); idVals(idx)];
end
or a more modern approach using DISCRETIZE (as Steven Lord mentioned):
function HD_cols = members2(Ic)
loadVals = [160000, 180000, 204000, 226000, 250000, 274000, 300000, 342000, 392000, 450000, 519000, 596000, 664000, 755000] ./ 100^4;
constVals = [ 537, 590, 649, 701, 755, 808, 863, 948, 1043, 1149, 1262, 1386, 1530, 1655] ./ 100^2;
idVals = [ 421, 463, 509, 551, 592, 634, 677, 744, 818, 900, 990, 1086, 1202, 1299];
midpoints = (loadVals(1:end-1) + loadVals(2:end)) / 2;
idx = discretize(Ic,[-Inf,midpoints,Inf]);
HD_cols = [loadVals(idx); constVals(idx); idVals(idx)];
end
  4 个评论
Stephen23
Stephen23 2025-9-4,17:32
编辑:Stephen23 2025-9-4,17:49
Take a look at those variable names and the meta-data that you put into them:
HD400_1299 = ..
% ^^^^ meta-data / data
Putting meta-data into variable names is a dead end, a shot in the foot. It means that any change in that data requires rewriting the code and/or using inefficient meta-programming commands. For example, after writing lots and lots of variables with meta-data in their names users often ask how they can efficiently iterate over them or process their data.
There is no efficient or simple way to do this, because of their data design:
In general using matrices and arrays to store meta-data (which is data) allows for much more efficient data access and operations, simpler code, easier debugging, etc. Generalising the code is much easier too.

请先登录,再进行评论。

更多回答(2 个)

Steven Lord
Steven Lord 2025-9-4,12:34
Take a look at the discretize function.

Matt J
Matt J 2025-9-4,13:16
编辑:Matt J 2025-9-4,13:53
function [HD_cols] = members(Ic);
HD = [ ...
160000/100^4, 180000/100^4, 204000/100^4, 226000/100^4, 250000/100^4, ...
274000/100^4, 300000/100^4, 342000/100^4, 392000/100^4, 450000/100^4, ...
519000/100^4, 596000/100^4, 664000/100^4, 755000/100^4;
537/100^2, 590/100^2, 649/100^2, 701/100^2, 755/100^2, ...
808/100^2, 863/100^2, 948/100^2, 1043/100^2, 1149/100^2, ...
1262/100^2, 1386/100^2, 1530/100^2, 1655/100^2;
421, 463, 509, 551, 592, ...
634, 677, 744, 818, 900, ...
990, 1086, 1202, 1299 ...
];
Limits=[ movmean(HD(1,:),[0,1],Endpoints='discard')) , inf];
HD_cols=HD(:, discretize(Ic,Limits) );
end

类别

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

Community Treasure Hunt

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

Start Hunting!

Translated by