How to index something in order
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Gadelhag M Omar Mohmed
am 24 Jun. 2019
Kommentiert: Gadelhag M Omar Mohmed
am 24 Jun. 2019
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 Kommentare
Akzeptierte Antwort
Bob Thompson
am 24 Jun. 2019
Bearbeitet: Bob Thompson
am 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 Kommentare
Bob Thompson
am 24 Jun. 2019
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)
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!