creating a new vector relative to the matrix
조회 수: 3 (최근 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에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!