How to pull a range of values out of an array using a loop

6 次查看(过去 30 天)
I am trying to use a loop to check the values stored in the array "tau". If the value is less than 1e8 I would like to store it in a new array called "acceptable" else the value should go into an array called "unacceptable" (for plotting purposes). I am trying to understand when things are being itterated in MATLAB. Since the variables J and tau were not iterated using a loop this is quite confusing coming from java since everything has to be iterated using loops in that lang. I do not know how to use the loop to check and pull values from "tau". Any help is greatly appreciated.
clc; clear ; clear all
F = 10; %Newtons
T = F*0.1 ; %Work
r = .001: 1e-4: .005; %mm
i = 0; %iterator
%acceptable = 1 %array to be made using values > 1e8
%unacceptable = 1 %array to be made using values < 1e8
J = (.5*pi)*r.^4 ; %Polar moment of inertia
tau = ((T*r)./J); %Torsional Sheer Stress
while i < 5
if tau < 1e8
acceptable = tau
else
unacceptable = tau
end
i = i + 1 ;
end
x1 = 0
x2 = .005
y = 1e8
plot(r,tau)
hold on
plot([x1,x2],[y,y])
xlim([0,5e-3])
ylim([0,5e8])

回答(2 个)

KSSV
KSSV 2020-2-18
Simply use:
idx = tau < 1e8 ;
In idx, 1 is acceptable and 0 is unacceptable. Read baout logical indexing. You need not run a loop in MATLAB.

Stephen23
Stephen23 2020-2-18
MATLAB is not Java. Using a loop would be entirely the wrong approach in MATLAB.
>> idx = tau < 1e8;
>> acceptable = tau(idx);
>> unacceptable = tau(~idx);

类别

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

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by