フィルターのクリア

Storage of first few values in an array but with an if condition

1 回表示 (過去 30 日間)
Sandip Ghatge
Sandip Ghatge 2020 年 5 月 18 日
編集済み: Sandip Ghatge 2020 年 5 月 21 日
I am stuck here, because when i write the following code to take in values lesser than or equal to 0.20 and greater than or equal to 0.12, it takes all the values of the first column <= 0.20 and >=0.12. The desired result is taking in first set of values, lesser than or equal to 0.2 and greater than or equal to 0.12 and not all the values.
The code is,
A = [0.25;0.12;0.18;0.21;0.26;0.18;0.19;0.25;0.26;0.12;0.18;0.21];
B = [A(A(:,1) <= 0.20 & A(:,1) >= 0.12,1)];
The output of this is,
B =
0.1200
0.1800
0.1800
0.1900
0.1200
0.1800
What i am desiring is
B =
0.12
0.18
  2 件のコメント
KSSV
KSSV 2020 年 5 月 18 日
Sandip Ghatge
Sandip Ghatge 2020 年 5 月 18 日
Thank you.
I am going through the command.

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

採用された回答

Rik
Rik 2020 年 5 月 18 日
編集済み: Rik 2020 年 5 月 18 日
If you want to find the first block of true in L, you can use this code:
A = [0.25;0.12;0.18;0.21;0.26;0.18;0.19;0.25;0.26;0.12;0.18;0.21];
L=A(:,1) <= 0.20 & A(:,1) >= 0.12;
ind1=find(L,1);%first location within L
ind2=find(diff(L)==-1,1);%last location within L
if isempty(ind2),ind2=numel(A);
B=A(ind1:ind2)
Original answer:
Assuming you also wanted the 0.19:
A = [0.25;0.12;0.18;0.21;0.26;0.18;0.19;0.25;0.26;0.12;0.18;0.21];
L=A(:,1) <= 0.20 & A(:,1) >= 0.12;%put in a different variable for readability
B = A(L);
B=unique(B,'stable');%don't sort values
  1 件のコメント
Sandip Ghatge
Sandip Ghatge 2020 年 5 月 18 日
Thanks for the answer.
I cannot modify this code to exclude 0.19.
I just want a set when the values of the matrix A first get <= 0.20 and >=0.12, the moment it gets out of these limits(which is at A(4,1) here) the storage into a different matrix say B should stop.

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

その他の回答 (1 件)

Guillaume Le Goc
Guillaume Le Goc 2020 年 5 月 18 日
You could use the find function, where you can specify the first n elements you want :
A = [0.25;0.12;0.18;0.21;0.26;0.18;0.19;0.25;0.26;0.12;0.18;0.21];
ids = find(A<=0.2 & A>=0.12, 2); % second argument specifies you want only the first 2 elements that match the condition
B = A(ids);
Or directly :
A = [0.25;0.12;0.18;0.21;0.26;0.18;0.19;0.25;0.26;0.12;0.18;0.21];
B = A(find(A<=0.2 & A>=0.12, 2));
  3 件のコメント
Rik
Rik 2020 年 5 月 20 日
If this answer doesn't solve your question, why did you accept it?
After your comment I have edited my answer. Did you see that?
Sandip Ghatge
Sandip Ghatge 2020 年 5 月 21 日
編集済み: Sandip Ghatge 2020 年 5 月 21 日
@Rik, I am sorry, i wanted to accept your answer, but by mistake i accepted this answer. Thanks for bringing it into notice. i am accepting your answer as it takes me to the closest what i was aiming for and also have learnt a new command.

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

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by