Creating a vector using conditions and a cell.

조회 수: 1 (최근 30일)
Julen Aramendi
Julen Aramendi 2021년 7월 15일
댓글: Stephen23 2021년 7월 16일
This is what I have in the workspace. I have 3 acceleration vectors (one column each): Ax (799x1 double), Ay(799x1 double) and Az(799x1 double). Also I have a cell called activities: activities (1x4 cell) with this code:
Activities={'Walking','Sitting','Standing','Laying'};
Walking=1;
Sitting=2;
Standing=3;
Laying=4;
What I want to do is to create another vector called "actid"(799x1 double) using this conditions:
if Ay>10
actid=1; %Walking
elseif Ay>5 & Ay<7.5
actid=2; %Sitting
elseif Ay<10 & Ay>9
actid=3; %Standing
elseif Ay<1 & Az>9
actid=4; %Laying
end
Running this code I can't get what I want.
I don't know if it is possible to do.
Thank you.
  댓글 수: 3
Julen Aramendi
Julen Aramendi 2021년 7월 16일
First of all, thanks for answering me.
Yeah, it is intentional. Yesterday I didn't explain very well what I want. What I want is that depending the values of the acceleration in X(Ax), Y(Ay) and Z(Az) axles, know if the person is walking, sitting, standing or laying. For that reason, in that case I don't need the acceleration in the axis X because with the other two acceleration axis (Y and Z) is enough.
I don't understand what you want to say with "operate on the entire vectors". Can you give me a simple example?
Thank you.
Stephen23
Stephen23 2021년 7월 16일
"operate on the entire vectors". Can you give me a simple example?"
Simon Chan's answer is an example. Indexing would be another vectorized approach. See:
The difference is thinking in terms of matrices/arrays, not individual elements.

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

채택된 답변

Simon Chan
Simon Chan 2021년 7월 15일
편집: Simon Chan 2021년 7월 15일
Try this:
Zeros in the matrix means all conditions are not meet
actid=(Ay>10) + (Ay>5 & Ay<7.5)*2 + (Ay<10 & Ay>9)*3 + (Ay<1 & Az>9)*4

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by