필터 지우기
필터 지우기

How to convert rows to logical

조회 수: 2 (최근 30일)
Simon Allosserie
Simon Allosserie 2022년 10월 27일
댓글: Simon Allosserie 2022년 11월 10일
I am filtering a set of values in a column vector in multiple steps. In the end, I get the row numbers of the values that should be kept, eg.:
A = 1:10;
%fitering happens
r = [2 7 8]; %the rows to keep
Now for subsequent calculations, I need A with only the filtered values, and 0's in the other values. At the moment I do it like this:
filter = ~min(logical(abs(A-r(:))),[],1)
filter = 1×10 logical array
0 1 0 0 0 0 1 1 0 0
Afiltered = A.*filter
Afiltered = 1×10
0 2 0 0 0 0 7 8 0 0
However this method isn't that elegant so I was wondering if there are some standard functions / quicker ways to get the same?
Due to the nature of the filters, it is not possible to work with masks that have the same length as A. r will always be shorter than A.

채택된 답변

Star Strider
Star Strider 2022년 10월 27일
Why not just:
A = 1:10;
%fitering happens
r = [2 7 8]; %the rows to keep
Afiltered = zeros(size(A));
Afiltered(r) = A(r)
Afiltered = 1×10
0 2 0 0 0 0 7 8 0 0
.
  댓글 수: 2
Cris LaPierre
Cris LaPierre 2022년 10월 28일
This is likely the better answer. Here, Afiltered is assigned the values in A. In my answer, it is assigned the row numbers. It only looks correct because, in this simplified example, the elements in A are the row numbers.
Simon Allosserie
Simon Allosserie 2022년 11월 10일
Okay I will look into the differences between the two approaches and see which renders the results the fastest for the data that I use. Thanks for coming back to this!

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

추가 답변 (1개)

Cris LaPierre
Cris LaPierre 2022년 10월 27일
I'm not aware of a special function for this, but perhaps a more readable way would be this?
A = 1:10;
r = [2 7 8];
Afiltered=zeros(1,length(A));
Afiltered(r)=r
Afiltered = 1×10
0 2 0 0 0 0 7 8 0 0
  댓글 수: 1
Simon Allosserie
Simon Allosserie 2022년 10월 27일
Yes this is indeed much more simple, I was making it way too complicated. Thanks for helping me out of my thinking rut!

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

카테고리

Help CenterFile Exchange에서 Matched Filter and Ambiguity Function에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by