フィルターのクリア

How to index something in order

2 ビュー (過去 30 日間)
Gadelhag M Omar Mohmed
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 Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by