insert a value into an array after it's created

50 views (last 30 days)
Hi i have this problem. I make an array of n elements. After i make that i scan it to see if all elements are of the same dimension(they are measurements of intervals, so they must be kinda stable). If this is not, and i find that one or more element of the array is way bigger than the other (so i miss a point for the intervals), i want to insert an element in the middle of the array shifting all the following elements.
This is my array [760 810 788 1650 798] which is simply the intervals between data in this other array [100 860 1570 2358 4058 4856]. I see that 4° element is too big while it's supposed to be stable, so clearly i miss an element in the second array, like 4106. I have to insert this i value i calculate between 2358 and 4058, shifting the rest of the array, and then do the same thing for the first array. How can i do that?

Answers (2)

TAB
TAB on 28 Sep 2011
Please give example of source array and result array which you want. It is not possible to enter the element in between existing array element, but you can construct new array using them.
For example Insert Arr2 after 2nd element of Arr1
Arr1=[760 810 788 1650 798];
Arr2=[1 2 3 4];
Arr1=[Arr1(1:2) Arr2 Arr1(3:end)];
[EDITED]
Insert Ar1 at Pos1, Ar2 at Pos2 & Ar3 at Pos3 of MainArr
MainArr=[10 20 30 40 50 60 70 80 90];
Ar1=[1 2];Pos1=2;
Ar2=[4 5 6];Pos2=6;
Ar3=[7 8 9 8];Pos3=8;
MainArr=[MainArr(1:(Pos1-1)) Ar1 MainArr(Pos1:(Pos2-1)) Ar2 MainArr(Pos2:(Pos3-1)) Ar3 MainArr(Pos3:end)]
Hope you got the logic. You can modify according to your need.

Corrado Giuliani
Corrado Giuliani on 28 Sep 2011
Mmm it's exactly what i need to do. But this creates another problem. I dont know how many elements i've missed in my analysis and in my arrays, so i search for them in a if-while loop. So obviously i cant use the same two arrays. I explain it better. I make this:
max= max(vet_intR)%vet_intR are value x(b)-value x(a)
min= min(vet_intR);
if max>=1.5*min
p=1;
while p<=max(size(vet_intR))
if vet_intR(p)==max
int=round((vet_istR(p+1)-vet_istR(p))/2);% vet_istR is
%the array from where i take the values of vet_intR
a=vet_istR(p);
b=a+int;
endint=b+100;
c=b-100;
MAX=max(c:endint);
while c<=endint
if x(c)==MAX
% then comes the problem. I get the value i need,which is both c than c-vet_istR(p), the first goes in vet_istR at (p+1) position, while the second goes in the vet_intR value at p position.
The problem is that maybe i dont have just one value to add, but a two or more maybe.
I hope you understand the problem.
  2 Comments
TAB
TAB on 29 Sep 2011
As I understood, you want have many arrays to be inserted in another array.
You can insert any number of element at any position. For example see edited part of my above answer.
TAB
TAB on 29 Sep 2011
It is not good practice to use matlab keywords and function names as your 'Variable name'.
max= max(vet_intR);
min= min(vet_intR); thses lines will not run at all.
Have you tried running your code?

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!