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

1 Ansicht (letzte 30 Tage)
Brad
Brad am 15 Dez. 2015
Kommentiert: Brad am 15 Dez. 2015
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.

Akzeptierte Antwort

Stephen23
Stephen23 am 15 Dez. 2015
Bearbeitet: Stephen23 am 15 Dez. 2015
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

Weitere Antworten (0)

Kategorien

Mehr zu Tables finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by