How to index something in order
1 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
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
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)
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!