How to index something in order

조회 수: 1 (최근 30일)
Gadelhag M Omar Mohmed
Gadelhag M Omar Mohmed 2019년 6월 24일
댓글: Gadelhag M Omar Mohmed 2019년 6월 24일
Hi Everyone
I have a matrix of 2301 by 5. I want to check if the number in the forth column is the same, if it yes generat a new column (sixth column) counting from 1 up to the forth column's value changes and then start counting again. For example:
x= [10 11 45 4 26; 15 45 78 4 25; 23 6 8 4 25; 12 58 47 4 3; 125 36 58 4 25; 89 56 87 5 25; 56 4 2 5 88; 45 78 25 5 36; 25 68 36 5 89; 68 48 79 6 45; 45 89 23 6 68; 15 48 26 6 99]
So, now what I expectd is :
y= [10 11 45 4 26 1; 15 45 78 4 25 2; 23 6 8 4 25 3; 12 58 47 4 3 4; 125 36 58 4 25 5; 89 56 87 5 25 1; 56 4 2 5 88 2; 45 78 25 5 36 3; 25 68 36 5 89 4; 68 48 79 6 45 1; 45 89 23 6 68 2 ; 15 48 26 6 99 3]
Thanks in advance

채택된 답변

Bob Thompson
Bob Thompson 2019년 6월 24일
편집: Bob Thompson 2019년 6월 24일
Something like this?
y = x;
y(1,end+1) = 1;
for i = 2:size(y,1)
if y(i,4) == y(i-1,4)
y(i,end) = y(i-1,end) + 1;
else
y(i,end) = 1;
end
end
**EDIT: Fixed a typo, should work now.
  댓글 수: 3
Bob Thompson
Bob Thompson 2019년 6월 24일
Sorry, there was a typo. Replace the following:
if y(1,4) == y(i-1,4)
with the following:
if y(i,4) == y(i-1,4)
Gadelhag M Omar Mohmed
Gadelhag M Omar Mohmed 2019년 6월 24일
Thank you very much Bob Nbob, it is working as I want now

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

추가 답변 (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