How to pull a range of values out of an array using a loop
조회 수: 1 (최근 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])
댓글 수: 0
답변 (2개)
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.
댓글 수: 0
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);
댓글 수: 0
참고 항목
카테고리
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!