creating a new vector relative to the matrix
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Przemyslaw Kaluski
el 9 de Mayo de 2015
Comentada: Nobel Mondal
el 11 de Mayo de 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 :)
0 comentarios
Respuesta aceptada
Nobel Mondal
el 9 de Mayo de 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
0 comentarios
Más respuestas (3)
Muhammad Usman Saleem
el 9 de Mayo de 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..
0 comentarios
Przemyslaw Kaluski
el 10 de Mayo de 2015
1 comentario
Nobel Mondal
el 11 de Mayo de 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 '='
Ver también
Categorías
Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!