How do I replace data in an array in a specific order?
2 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
采纳的回答
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 Center 和 File Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!