필터 지우기
필터 지우기

Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

how to record the changing of columns

조회 수: 1 (최근 30일)
xueqi dong
xueqi dong 2017년 5월 31일
마감: MATLAB Answer Bot 2021년 8월 20일
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

답변 (1개)

alice
alice 2017년 6월 1일
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!

이 질문은 마감되었습니다.

태그

Community Treasure Hunt

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

Start Hunting!

Translated by