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

 Accepted Answer

Bob Thompson
Bob Thompson on 24 Jun 2019
Edited: Bob Thompson on 24 Jun 2019
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 Comments

Thank you Bob Nbob
It is working but it is not giving me waht I am looking for. What I want is as soon as the value in the fourth column is the same (in this example is 4), MATLAB shoud start counting in the sixth column 1, 2, 3, 4, 5 and in the sixth row (whne the value in the fourth column changed to 5), it should statrt counting aging from 1 until the last line with the value 5 and so on.
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)
Thank you very much Bob Nbob, it is working as I want now

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!