inserting into certain locations in array

2 次查看(过去 30 天)
A=[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15]
B = [ 10 20 30]
C= 3 % is the position where I want to insert B in A
I = 2 % is the number of times or multiple
output must be:
F = [ 1 2 3 10 20 30 4 5 6 10 20 30 7 8 9 10 11 12 13 14 15]
if C= 2 and I = 4
then, F = [ 1 2 10 20 30 3 4 10 20 30 5 6 10 20 30 7 8 10 20 30 9 10 11 12 13 14 15]

采纳的回答

Bob Thompson
Bob Thompson 2019-2-12
F = A;
for i = 1:I
F = [F(1:C*i+length(B)*(i-1)),B,F(C*i+length(B)*(i-1)+1:end)];
end

更多回答(1 个)

liju Abraham
liju Abraham 2019-2-12
Thanks for the response, I asked this question to try to understand how to solve a larger problem where I have data for two plots first is a general load and second is a special load. I have to add this special load just the same way like the array. My inputs are the time location 'C' where I want to add the special load and second input is the number of times I want to add this special load 'I'
I am able to add the special load once on the general load at any location but I would like some advice on how to go through with it for the same way like a array problem
  3 个评论
liju Abraham
liju Abraham 2019-2-12
Sorry about that, I am implementing the same logic for nx2 matrix's but I am am getting error especially since I have to plot the results. When I move the special load to a desired location in the general load I also have to add column 1 (time) with the position moved.
Bob Thompson
Bob Thompson 2019-2-12
Yes, if you are plotting against another array you will need to account for the extra entries in your F array.
If you are having an error feel free to post it here.
FYI: I don't know if a mod will see this, but they prefer to have 'answers' be actual answers, not comment responses. They may ask you to move your answer to a comment under my answer, or will move it for you.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

产品


Translated by