How to convert rows to logical
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
Simon Allosserie
il 27 Ott 2022
Commentato: Simon Allosserie
il 10 Nov 2022
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)
Afiltered = A.*filter
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.
0 Commenti
Risposta accettata
Star Strider
il 27 Ott 2022
Why not just:
A = 1:10;
%fitering happens
r = [2 7 8]; %the rows to keep
Afiltered = zeros(size(A));
Afiltered(r) = A(r)
.
2 Commenti
Cris LaPierre
il 28 Ott 2022
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.
Più risposte (1)
Cris LaPierre
il 27 Ott 2022
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
Vedere anche
Categorie
Scopri di più su Filter Analysis in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!