creating a new vector relative to the matrix
2 次查看(过去 30 天)
显示 更早的评论
I have an example matrix:
1 19:20:43 DC 7,102 V AUTO
2 19:20:44 DC 7,103 V AUTO
3 19:20:44 DC 7,105 V AUTO
4 19:20:45 DC 7,105 V AUTO
5 19:20:46 DC 6,105 V AUTO
6 19:20:47 DC 1,105 V AUTO
7 19:20:48 DC 0 V AUTO
8 19:20:48 DC 0 V AUTO
9 19:20:49 DC 1 V AUTO
10 19:20:50 DC 7,105 V AUTO
I’ d like to create a new vector, which will assign to time from a first row of matrix (19:20:43) value 1, to time from second row (19:20:44) – value 2 e.c.t.. But if the time value repeats, assign the same value for both time values. For matrix above, new vector should be t=[1;2;2;3;4;5;6;6;7;8].
I should probably use loop for, and function if, but really don’t know how to do it.
I hope you understood my problem :)
0 个评论
采纳的回答
Nobel Mondal
2015-5-9
% A = your 10x6 original matrix;
uRows = unique(A, 'rows');
result = zeros(6,1);
for i=1:size(uRows, 1)
cols = find(ismember(A, uRows(i,:), 'rows'));
result(cols) = i;
end
0 个评论
更多回答(3 个)
Muhammad Usman Saleem
2015-5-9
clear some questions for me. what is the size of the following matrix? how many rows and column it has?
1 19:20:43 DC 7,102 V AUTO
2 19:20:44 DC 7,103 V AUTO
3 19:20:44 DC 7,105 V AUTO
4 19:20:45 DC 7,105 V AUTO
5 19:20:46 DC 6,105 V AUTO
6 19:20:47 DC 1,105 V AUTO
7 19:20:48 DC 0 V AUTO
8 19:20:48 DC 0 V AUTO
9 19:20:49 DC 1 V AUTO
10 19:20:50 DC 7,105 V AUTO
tell me are these 10 different matrixs or only single one? As matrix has same data type.i am getting confuse you are using numeric and string in matrix.This should be structure case..
0 个评论
Przemyslaw Kaluski
2015-5-10
1 个评论
Nobel Mondal
2015-5-11
This code would strictly hold up for the values that you have shown in the question. But, say what about when the minutes (column 5) change as well.
Anyway, assuming z is your original 10x6 matrix, the condition checking line is incorrect.
if z(i+1,6)==z(i,6); % Use '==' not the assignment operation '='
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!