A more efficient way to do this calculation
Mostrar comentarios más antiguos
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 comentarios
Sam Chak
el 4 de Sept. de 2025
I believe you are using the logical indexing technique for data segregation. Are you looking for ways to make the code run much faster, rather than checking true/false values one by one?
Scott Banks
el 4 de Sept. de 2025
Scott Banks
el 4 de Sept. de 2025
Respuesta aceptada
Más respuestas (2)
Steven Lord
el 4 de Sept. de 2025
2 votos
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
Categorías
Más información sobre Tables en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!