How to separate a vector into two different vectors?

조회 수: 35 (최근 30일)
Kristin Aldridge
Kristin Aldridge 2021년 12월 2일
댓글: Star Strider 2021년 12월 2일
I have a vector named "age_vec" that I would like to piece into two groups, those above 37.5 and those below, and place those numbers into another vector. I keep trying to run this through a for loop but it puts all the numbers in the same vector, either all placed in agegreater or ageless, or I even tried just getting it to count those above and those below and it keeps putting everything into just one vector/variable.
Thank you.
age_vec =[ 21 18 57 52 20 22 23 21.50 38 31 30 29 58 53 21.75 86 55]
%Counting those above and those below
ageless=0;
agemore=0;
for i=1:length(age_vec)
if age_vec > 37.5
ageless= ageless + 1
else agemore= agemore + 1
end
end
%Placing into vectors
ageless=[];
agemore=[];
for i=1:length(age_vec)
if age_vec < 37.5
ageless=[ageless age_vec] + 1
else agemore=[agemore age_vec] + 1
end
end

채택된 답변

Star Strider
Star Strider 2021년 12월 2일
Try this —
age_vec =[ 21 18 57 52 20 22 23 21.50 38 31 30 29 58 53 21.75 86 55];
Lv = age_vec > 37.5; % Logical Vector
age_more = age_vec(Lv)
age__more = 1×7
57 52 38 58 53 86 55
age_less = age_vec(~Lv)
age_less = 1×10
21.0000 18.0000 20.0000 22.0000 23.0000 21.5000 31.0000 30.0000 29.0000 21.7500
This uses ‘logical indexing’. See the docuemtation section on Matrix Indexing for details.
.
  댓글 수: 2
Kristin Aldridge
Kristin Aldridge 2021년 12월 2일
Thank you so much, this works great.
Star Strider
Star Strider 2021년 12월 2일
As always, my pleasure!
.

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

추가 답변 (3개)

Image Analyst
Image Analyst 2021년 12월 2일
편집: Image Analyst 2021년 12월 2일
Try this:
age_vec =[ 21 18 57 52 20 22 23 21.50 38 31 30 29 58 53 21.75 86 55]
% Find indexes that are more than 37.5
moreIndexes = age_vec > 37.5
% Extract into two new vectors.
ageless=age_vec(~moreIndexes)
agemore=age_vec(moreIndexes)

Voss
Voss 2021년 12월 2일
Probably the easiest way to do what you want is to use logical indexing. First make a vector of logicals that say whether each element of age_vec is greater than 37.5 or not:
is_greater = age_vec > 37.5;
Then make two new vectors by separating the elements of age_vec according to the corresponding value in is_greater:
age_more = age_vec(is_greater);
age_less = age_vec(~is_greater);
If you really want or need to use a for loop, let me know and I can show you how that would work, but this way with logical indexing is much more concise.

James Tursa
James Tursa 2021년 12월 2일
편집: James Tursa 2021년 12월 2일
Others have already pointed out better ways of doing this. But to answer your question as to why your current code is not working, it is because you need to use the index i in your code. E.g.,
if age_vec(i) < 37.5
ageless = [ageless age_vec(i)];
else
agemore = [agemore age_vec(i)];
end
This will incrementally build up the ageless and agemore vectors, but at each iteration you will have to deep copy one of the vectors, so the performance will be severly impacted as the size of age_vec gets large. Hence the desire to use a different method as others have suggested.

카테고리

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