Filter löschen
Filter löschen

Info

Diese Frage ist geschlossen. Öffnen Sie sie erneut, um sie zu bearbeiten oder zu beantworten.

how to record the changing of columns

1 Ansicht (letzte 30 Tage)
xueqi dong
xueqi dong am 31 Mai 2017
Geschlossen: MATLAB Answer Bot am 20 Aug. 2021
Hi, I have a matrix looks like the attached file. As you can see, it is 219*6 matrix and only contains 1 and 0. The data shows the location of an object in timestamps. The 6 columns represents 6 locations and rows represents the time. For example, when t=10, the subject is hitting location 2, so we have example(10,2)=1. When t=9, the object is hitting location 1, so we have example(9,1)=1...What I would like to achieve is to record the time span that the object is change from one location to another location. As there are 6 locations, so we have 15 ways of path, i.e., 1 to 2, 1 to 3, 2 to 3.... For example, From t=1 to t=81, the object is moving between location 1 and 2. so we have D12(1)=81.
Could you offer some advice about how to do this in general by coding? At the moment, I am just using my eyes and counting...This is clearly not the most efficient way.
Thank you, Xueqi

Antworten (1)

alice
alice am 1 Jun. 2017
Hello,
I am not sure about what you want to achieve exactly, but I hope this helps anyway:
  • working with a location array (vector of an integer between 1 and 6 representing the location at each time step) would be easier. You can transform your matrix in a location array looking at which column of your matrix is not zero. One way of doing it:
locations = arrayfun(@(iii) find(example(iii,:)), 1:size(example,1))';
  • then you can get all the transitions looking at when the location changes:
transitions_time = find(diff(locations))+1;
With your example, I got the above table of transitions using the following code:
% location array
locations = arrayfun(@(nRow) find(example(nRow,:)), 1:size(example,1))';
% transitions
transitions_time = find(diff(locations))+1; % time of the transition
initial_location = locations(transitions_time-1); % location before the transition
final_location = locations(transitions_time); % location after the location
transitions_name = cellstr(strcat('transition #',num2str((1:length(transitions_time))')))'; % label of the transition
% table
T = table(transitions_time,initial_location,final_location,'RowNames',transitions_name)
You should be able to get what you want from there.
Good luck!

Diese Frage ist geschlossen.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by