필터 지우기
필터 지우기

creating a new vector relative to the matrix

조회 수: 3 (최근 30일)
Przemyslaw Kaluski
Przemyslaw Kaluski 2015년 5월 9일
댓글: Nobel Mondal 2015년 5월 11일
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 :)

채택된 답변

Nobel Mondal
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

추가 답변 (3개)

Muhammad Usman Saleem
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..

Przemyslaw Kaluski
Przemyslaw Kaluski 2015년 5월 9일
It was my .xls data file before importing it to matlab, sorry for that.
This is my one matrix 10x6
0 0 0 19 20 43
0 0 0 19 20 44
0 0 0 19 20 44
0 0 0 19 20 45
0 0 0 19 20 46
0 0 0 19 20 47
0 0 0 19 20 48
0 0 0 19 20 48
0 0 0 19 20 49
0 0 0 19 20 50
4th column is hour, 5th is minute, 6th is second of measure. I'd like to create a new vector t, which will assign to time value from a first row of matrix (0 0 0 19 20 43) value 1, to time value from second row (0 0 0 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].

Przemyslaw Kaluski
Przemyslaw Kaluski 2015년 5월 10일
It works, thank you.
I made another script by myself, but it doesn't run, don't know why. Could you tell me what is wrong with it? Sorry for messing, I'm new at Matlab and I'd like to know where I make mistakes :)
t=[];
t(1,1)=1;
for i=1:length(z);
if z(i+1,6)=z(i,6)
t(1+i,1)=i;
else
t(1+i,1)=i+1;
end
  댓글 수: 1
Nobel Mondal
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 CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by