MATLAB Answers

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

2 views (last 30 days)
Tsansoterra
Tsansoterra on 18 Feb 2020
Answered: Stephen Cobeldick on 18 Feb 2020
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 Comments

Sign in to comment.

Answers (2)

KSSV
KSSV on 18 Feb 2020
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 Comments

Sign in to comment.


Stephen Cobeldick
Stephen Cobeldick on 18 Feb 2020
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 Comments

Sign in to comment.

Sign in to answer this question.

Tags

Products


Translated by