Efficient way/best practice to slice array under a logical condition as well as other conditions

조회 수: 33 (최근 30일)
I did not know how to properly title this question, so please bear with me.
Simply put, with the following two arrays:
A = [0 1 0 1 0 0 0 1]'
B = [800 1 1 60;
900 3 0 30;
900 4 0 30;
900 6 0 30;
900 7 0 30;
900 11 0 30;
900 1 2 60;
1000 1 2 60;
]
Note that A and B have the same number of columns.
I would like, for example, to slice B under the following constraints:
  • A == 1
  • B(:,3) ~= 0
  • Keep only columns [2 3 4] of B
This is the way I am doing it at the moment:
B_sliced = B(A==1 & B(:,3)~=0,[2 3 4]);
Alternatively, one could do:
B_sliced = B(logical(A) & B(:,3)~=0,[2 3 4]);
With tic toc and large matrices, I am not noticing much of a difference in performance.
Is there a best practice regarding combining logical with non-logical conditions? Should I keep something in mind, or it doesn't really matter which way I go? Is there a way to make any of these more efficient by using "find"?
I hope you can enlighten me a bit! Thanks.
  댓글 수: 1
Bruno Luong
Bruno Luong 2020년 10월 4일
편집: Bruno Luong 2020년 10월 4일
Don't split the hair, it doesn't really matter.
Just do the way te code looks most readable to you.

댓글을 달려면 로그인하십시오.

답변 (1개)

madhan ravi
madhan ravi 2020년 10월 4일
What you have is more efficient.

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by