Effacer les filtres
Effacer les filtres

creating a new vector relative to the matrix

2 vues (au cours des 30 derniers jours)
Przemyslaw Kaluski
Przemyslaw Kaluski le 9 Mai 2015
Commenté : Nobel Mondal le 11 Mai 2015
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 :)

Réponse acceptée

Nobel Mondal
Nobel Mondal le 9 Mai 2015
% 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

Plus de réponses (3)

Muhammad Usman Saleem
Muhammad Usman Saleem le 9 Mai 2015
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 le 9 Mai 2015
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 le 10 Mai 2015
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 commentaire
Nobel Mondal
Nobel Mondal le 11 Mai 2015
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 '='

Connectez-vous pour commenter.

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by