필터 지우기
필터 지우기

Get every 4 values of a vector and change values

조회 수: 2 (최근 30일)
Nikolas Spiliopoulos
Nikolas Spiliopoulos 2022년 1월 4일
댓글: Nikolas Spiliopoulos 2022년 1월 4일
Hi all,
I have a vector like this
a= [1 1 0 0 1 1 1 1 0 1 0 1 1 0 0 0 0 0 0 0 1 1 0 0]
So I would like to take every four values in a group, check if there are any ones in it, and if yes change the four values to 1. otherwise leave zeros.
So the desired vector will be:
b= [1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 1 1 1 1]
can you help me with that?
thanks in advance,
Nikolas

채택된 답변

DGM
DGM 2022년 1월 4일
편집: DGM 2022년 1월 4일
Maybe something like this
a = [1 1 0 0 1 1 1 1 0 1 0 1 1 0 0 0 0 0 0 0 1 1 0 0]
a = 1×24
1 1 0 0 1 1 1 1 0 1 0 1 1 0 0 0 0 0 0 0 1 1 0 0
b = repelem(any(reshape(a,4,[]),1),4)
b = 1×24 logical array
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 1 1 1 1
I'm assuming the input is nominally binarized already (i.e. either logical class or numeric class but integer-valued and constrained to [0 1]). If otherwise, you'll have to explicitly convert it to logical using some desired test.
If the output needs to be numeric class, cast it using double().

추가 답변 (1개)

KSSV
KSSV 2022년 1월 4일
a = [1 1 0 0 1 1 1 1 0 1 0 1 1 0 0 0 0 0 0 0 1 1 0 0] ;
b = [1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 1 1 1 1] ;
idx = bsxfun(@plus, (1 : 4), (0 : numel(a) - 4).');
a = a(idx) ;
iwant = any(a,2)'
iwant = 1×21 logical array
1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 1 1 1 1

카테고리

Help CenterFile Exchange에서 Logical (Boolean) Operations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by