Finding values in an Array

조회 수: 5 (최근 30일)
Aldo Amaya
Aldo Amaya 2014년 1월 30일
댓글: Aldo Amaya 2014년 1월 30일
If I have an array such as
Subject= [126;156;1992, 203, 186] % First two digits determine group, additional numbers are specific subject within that group.
Is there a method to only read the first two values of each row in order to determine what group the subject is?
Group= [12;15;19;20;18] %%%This is what I would like the result to be.

채택된 답변

Jos (10584)
Jos (10584) 2014년 1월 30일
Apply some arithmetic:
Subject = [126 156 1992 203 186 10 100 100000]
Group = fix(Subject ./ (10.^floor(log10(Subject)-1)))
  댓글 수: 1
Aldo Amaya
Aldo Amaya 2014년 1월 30일
Thank you, this worked perfectly :)

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

추가 답변 (2개)

Walter Roberson
Walter Roberson 2014년 1월 30일
Group = floor(Subject ./ 10);
  댓글 수: 1
Aldo Amaya
Aldo Amaya 2014년 1월 30일
Thanks for the reply, the only issue is that they are not all the same number of elements. Subjects elements range from 3-5 currently.

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


Aldo Amaya
Aldo Amaya 2014년 1월 30일
편집: Aldo Amaya 2014년 1월 30일
This was my solution but I wish there was a cleaner way to do it
Group = floor(Namex2 ./ 10);
i=1;
while i<=length(Group)
if Group(i)>=100
Group(i)=floor(Group(i)./ 10);
else
end
i=i+1;
end

카테고리

Help CenterFile Exchange에서 Cell Arrays에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by