Finding the first element of a matrix that satisfy a condition in a frequently manner

38 ビュー (過去 30 日間)
shahin mohammadnejad
shahin mohammadnejad 2018 年 9 月 21 日
編集済み: Adam Danz 2018 年 9 月 24 日
Hello,
I have a large matrix of data (10000x6). As you can see in the following I am imposing a condition on my data, and this condition is true in a frequent manner. The thing is I want to extract just the first element whenever the condition is true (the marked points in the image) and exclude the other elements which satisfy the condition. I would be very thankful if someone could tell me how can I do this.
Thank you,
Shahin
y_coordinate=D(6:N_atom:end,:); TR=zeros(size(y_coordinate)); aa=1; for jj=1:length(y_coordinate) if y_coordinate(jj,2)<31.35 && y_coordinate(jj,4)<0 TR(aa,:)=y_coordinate(jj,:); aa=aa+1; end end

回答 (3 件)

Adam Danz
Adam Danz 2018 年 9 月 21 日
編集済み: Adam Danz 2018 年 9 月 23 日
... I want to extract just the first element whenever the condition is true...
This line below will return the index value of the first row that is true.
% Find the first index where condition is true (empty if none are true)
firstTrueIdx = find(y_coordinate(:,2)<31.35 & y_coordinate(:,4)<0, 1);
Now you can use firstTrueIdx| to identify the row number of that element. If it's empty, that means there were no true values.

Bruno Luong
Bruno Luong 2018 年 9 月 22 日
編集済み: Bruno Luong 2018 年 9 月 22 日
Your loop is equivalent to
condmeet = y_coordinate(:,2)<31.35 & y_coordinate(:,4)<0;
TR = y_coordinate(condmeet ,:);
If you want to find the first element then
ifirst = find(condmeet,1,'first');
FirstTR = y_coordinate(ifirst ,:);
it returns empty array if no row meets the condition.
  3 件のコメント
Bruno Luong
Bruno Luong 2018 年 9 月 23 日
Not sure about your comment Adam: the other answers need loop (with index jj), mine does not. Or you make a type with jj?
Adam Danz
Adam Danz 2018 年 9 月 23 日
ah, yeah... didn't mean to include the jj. good catch.

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


shahin mohammadnejad
shahin mohammadnejad 2018 年 9 月 23 日
Dear Adam,
Thank you for your response. I think because the plot of my data was not attached in my question, you couldn't catch my problem properly. Using the commands that you suggested, I get only the first element which satisfies the the condition. The thing is as you can see in the attached image, my data satisfy the condition many times (two of them are shown in the image), and I want whenever the condition is true I capture the first true value. For example, in this image I want the indices of both indicated points. Thank you in advance for your time and help.
Regards,
Shahin
  6 件のコメント
shahin mohammadnejad
shahin mohammadnejad 2018 年 9 月 24 日
Yes, you are right. But by using a loop, I can directly substitute the features of particles from y_coordinate matrix to TR matrix.
Adam Danz
Adam Danz 2018 年 9 月 24 日
編集済み: Adam Danz 2018 年 9 月 24 日
Oh, that could be achieved by changing the last line of my code above.
condmeetIdx = find(condmeet);
deltaIdx = [diff(condmeetIdx),0];
condmeetIdx(deltaIdx==1) = [];
TR = zeros(size(y_coordinate));
TR(condmeetIdx,:) = y_coordinate(condmeetIdx, :);

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

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by