combining (concat) matrix in loop to get desired output plot
1 次查看(过去 30 天)
显示 更早的评论
Hi,
I have two plots gotten from (general_load and special_load), they are time vs force. I want to create a loop where I can concat/merge the special load multiple times into the general load where N is the first position on the general_load and i is the number of times (the distance between the special loads must be N) .
Here is where it gets tricky since I want to keep the plots intact the time must be adjusted according ( eg, when the 1st special_load is added at t=50sec to the general load the entire time column in the matrix must be added by 50 such that there is a seamless connection.
A=xlsread('general_load.xlsx');
B=xlsread('test_gust_special_load');
a_length = A(end,1);
b_length = B(end,1);
N = 10;
%% for one special load in the general i used this approach
C1 = A(A(:,1)<=N,:);
C2 =[B(:,1)+N,B(:,2)];
C3 =A(A(:,1)>N,:);
C4=[C3(:,1)+b_length,C3(:,2)];
CT = vertcat(C1,C2);
%% but I am unable to get the for loop
for i= C4(1,1)+N:N:a_length
P1 =C4(C4(:,1)<=i,:);
P2 =[B(:,1)+i,B(:,2)];
PT = vertcat(P1,P2);
c_length=PT(end,1);
C4=PT(PT(:,1)+c_length,PT(:,2));
end
Z = vertcat(CT,C4);
plot(Z(:,1),Z(:,2));
0 个评论
回答(1 个)
Bob Thompson
2019-2-20
编辑:Bob Thompson
2019-2-20
I suspect the issue you're having is because you're using the same C4 without attaching it to Z each time in the loop. This means that instead of attaching each segment of your special and general you only get a final segment. If this is incorrect, then please feel free to expand what you mean by 'I'm unable to get the for loop'.
Glancing over your work I would suggest these changes.
A=xlsread('general_load.xlsx');
B=xlsread('test_gust_special_load');
a_length = A(end,1);
b_length = B(end,1);
N = 10:10:a_length; % I think it's worth it to just start here.
% You don't need to do the first round outside the loop.
mark(1) = 0; % Keep track of where we are in the A matrix
mark(2) = 0; % Keep track of new time values
C = []; % Initialize your end matrix
for i = 1:length(N)
A_seg = A( A(:,1) > mark(1) & A(:,1) <= N(i) , :);
mark(1) = A_seg(end,1);
A_seg(:,1) = A_seg(:,1) + mark(2);
B_seg = B;
B_seg(:,1) = B_seg(:,1) + A_seg(end,1);
C = [C;A_seg;B_seg];
mark(2) = B(end,1)*i; % EDIT**
end
5 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!