How do I replace data in an array in a specific order?

조회 수: 2 (최근 30일)
Brad
Brad 2015년 12월 15일
댓글: Brad 2015년 12월 15일
I'm attempting to write a chunk of code that takes a 5 x 2 array of doubles, locate specific times in the first column of data, and replaces them with updated values. The code I am working with is as follows;
clear all;
clc;
% 5 x 2 array of doubles
% column 1 contains state times in UTC
% Column 2 is time tagged data
UTC_Data = [86100 1; 86200 2; 500 3; 86400 4; 1000 5];
% Assign state times
State_Times = UTC_Data(:,1);
% Assign an initial scenario time in UTC
I_Time = 86000;
% Calculate the time difference between 86400 and I_time
T_Diff = 86400 - I_Time;
ind1 = find(State_Times > I_Time);
if size (ind1) > 0
UTC_Non_Rollover = State_Times(ind1) - I_Time;
else
UTC_Non_Rollover = [ ];
end
ind2 = find(State_Times < I_Time);
if size (ind2) > 0
UTC_Rollover = State_Times(ind2) + T_Diff;
else
UTC_Rollover = [ ];
end
The resulting 5 x 2 array ,B, should look like this:
100 1
200 2
900 3
400 4
1400 5
Is there a way to get the applicable times in their proper order in B?
Thank you.

채택된 답변

Stephen23
Stephen23 2015년 12월 15일
편집: Stephen23 2015년 12월 15일
Method One: rem:
UTC_Data = [86100 1; 86200 2; 500 3; 86400 4; 1000 5];
I_Time = 86000;
T_Diff = 86400 - I_Time;
%
C = UTC_Data;
C(:,1) = rem(C(:,1),I_Time) + T_Diff*(C(:,1)<I_Time)
Creates the output C:
C =
100 1
200 2
900 3
400 4
1400 5
Method Two: Logical Indexing:
B = UTC_Data;
idn = UTC_Data(:,1)>I_Time;
B(idn,1) = B(idn,1) - I_Time;
idr = UTC_Data(:,1)<I_Time;
B(idr,1) = B(idr,1) + T_Diff
creates the variable B
B =
100 1
200 2
900 3
400 4
1400 5

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by