extracting values from matlab double arrays

I have bunch of x1, x2, ... xn values as such as given below. I want to only extract those values for x1, x2, ..., xn only if values are <=1.5. How can I use for loop to do it? Could you kindly help? Thank you.
x1 =
0.1318
0.6027
0.7553
0.9645
1.0216
1.3936
1.4632
1.7074
1.8658
1.9327
x2=
0.0109
0.0301
0.0947
0.5086
0.6148
0.6889
0.7736
0.9207
1.0645
1.2933
1.4033
1.5021
1.6798
1.8843
1.9660
1.9904

 採用された回答

Cameron
Cameron 2023 年 1 月 12 日

0 投票

x1(x1 <= 1.5)
x2(x2 <= 1.5)
%...
xn(xn <= 1.5)

3 件のコメント

Cameron
Cameron 2023 年 1 月 12 日
If you're asking about how to get the value for dynamic variable names, you can do this
%create some random data between 1 and 2
x1 = rand(20,1) + 1;
x2 = rand(20,1) + 1;
x3 = rand(20,1) + 1;
x4 = rand(20,1) + 1;
x5 = rand(20,1) + 1;
NumVariables = 5; %since x5 is the largest, there are 5 variables
%loop through all
for n = 1:NumVariables
%this will make x1(x1 <= 1.5), x2(x2 <= 1.5),..., xn(xn <= 1.5)
str = ["x" + num2str(n) + "(x" + num2str(n) + "<= 1.5)"];
NewValue = eval(str); %evaluate the value of str
end
sam
sam 2023 年 1 月 12 日
Thank you. It helps
Steven Lord
Steven Lord 2023 年 1 月 12 日
Can you dynamically create variables with numbered names like x1, x2, x3, etc.? Yes.
Should you do this? The general consensus is no. That Answers post explains why this is generally discouraged and offers several alternative approaches.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

質問済み:

sam
2023 年 1 月 12 日

コメント済み:

2023 年 1 月 12 日

Community Treasure Hunt

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

Start Hunting!

Translated by