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

8 ビュー (過去 30 日間)
Tsansoterra
Tsansoterra 2020 年 2 月 18 日
回答済み: Stephen23 2020 年 2 月 18 日
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 Center および File ExchangeProgramming についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by