insert a value into an array after it's created
50 views (last 30 days)
Show older comments
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?
0 Comments
Answers (2)
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.
0 Comments
Corrado Giuliani
on 28 Sep 2011
2 Comments
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
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?
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!