Conditional statements on array.

조회 수: 73 (최근 30일)
Santos García Rosado
Santos García Rosado 2021년 1월 12일
댓글: Santos García Rosado 2021년 1월 12일
Hello MathWorks community!
Could someone give me a hand?
I'm struggling on a loop involving a conditional statement in order to get a particular array.
This is my Input array (1x24):
Input = [0,0,0,0,0,5,6,7,25,68,0,0,0,0,0,36,221,78,14,16,96,75,0,0]
What I'm trying to check is if there is any value bigger than 100 on a 12 range positions of the array. In case the statement is true, I would like to make the 12 range values equal 1, otherwise equal 0. (Note: the array input dimensions will always be a multiple of 12)
The Output on this particular example should look like this:
Output = [0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1]
The first 12 positions are equal to 0 because there aren't any values bigger than 100. But from 13 to 24 postions are equal to 1, because 221 is bigger than 100.
I hope I've explained my-self well enough.
Thanks for the help!

채택된 답변

Cris LaPierre
Cris LaPierre 2021년 1월 12일
This looks like a job for cummax, along with a logical array (ch 12 MATLAB Onramp).
Input = [0,0,0,0,0,5,6,7,25,68,0,0,0,0,0,36,221,78,14,16,96,75,0,0];
B = cummax(Input);
Output = B >= 100
Output = 1x24 logical array
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1
  댓글 수: 2
Cris LaPierre
Cris LaPierre 2021년 1월 12일
편집: Cris LaPierre 2021년 1월 12일
I was too quick and didn't catch the intervals of 12.
Assuming your vector length is always a multiple of 12, it doesn't get too much more complicated.
Input = [0,0,0,0,0,5,6,7,25,68,0,0,0,0,0,36,221,78,14,16,96,75,0,0];
% Convert to a matrix with 12 rows, n columns
B2 = reshape(Input,12,[]);
% Determine the value of each column
BM = max(B2)>100
BM = 1x2 logical array
0 1
% create new matrix of 0s and 1s
BM = BM.*ones(size(B2));
% Reshape back to a vector
Output = reshape(BM,1,[])
output = 1×24
0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1
Santos García Rosado
Santos García Rosado 2021년 1월 12일
Great! I was trying to find a way using the cummax documentation you provided. Your code works great anyway. Thank you so much Cris!

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

추가 답변 (2개)

David Hill
David Hill 2021년 1월 12일
i=reshape(Input,12,[])'>100;
Output=zeros(size(i));
for k=1:size(i,1)
if nnz(i(k,:))>0
Output(k,:)=1;
end
end
Output=Output';
Output=Output(:)';
  댓글 수: 1
Santos García Rosado
Santos García Rosado 2021년 1월 12일
Thank you for your help David. This works aswell.

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


Mathieu NOE
Mathieu NOE 2021년 1월 12일
hello Santos
this is one way to do it
split the input vector in 12 long extracts, then search for any value inside that is above your threshold and do some outputs (0 or 1's vectors)
not the fanciest code but it works
Input = [0,0,0,0,0,5,6,7,25,68,0,0,0,0,0,36,221,78,14,16,96,75,0,0];
ll = length(Input);
threshold = 100;
offset = 12;
loops = fix(ll/offset);
Output = [];
for ci = 1:loops
ind = 1+(ci-1)*offset;
input_extract = Input(ind:ind+offset-1);
if any(input_extract >= threshold)
out = ones(1,offset);
else
out = zeros(1,offset);
end
Output = [Output out];
end
  댓글 수: 1
Santos García Rosado
Santos García Rosado 2021년 1월 12일
You're right. Not the fanciest but still works. Thank you!

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

카테고리

Help CenterFile Exchange에서 Install Products에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by