add rows between two indexes in an array

I have an array where the cell values ascend from 1 to 300. Some values occur more often than others.
For values which come up less than 20 times, I would like to add enough of them to get to 20.
Example:
array1 = [1; 1; 1; 2; 2; 2; 2; 2; 3; 3; 3;] % snipit of the array
given that there are 20 of the 1s and 20 of the 3s I am missing 15 of the 2s (I only have 5 of the 2s in the array currently)
In another array I have the index of the values for which there are not 20, as well as the number of indexes missing to get to 20.
array2 = [401,15 ; 516,4 ; 1117, 11]
Since adding 15 indexes to 401 will shift the next index for which I add values by 15, the following could tackle that problem: an array which adds the number of shifts consecutively
for j=2:length(array2)
array3(j,1) = array2(i,2)+array2(i-1,2);
end
%produces following array:
array3=[0; 19; 30]
so now, the question is how do I put the following into actually working code:
for i=1:length(array2)
array1(array2(i+array3(i),1)) add array2(i,2)
end
any help would be very much appreciated!!

6 Kommentare

hosein Javan
hosein Javan am 12 Aug. 2020
your question is not clear. I suggest you display your original matrix and the matrix you expect the code to yield.
your title says "add row between two indices" which is unclear. you only can insert another row between two rows,
for the example with cell value 2:
array1 in the current state hast 20 ones for index 1 through 19, then 5 twos for index 20 through 24, then 20 threes for index 25 through 44.
array1 = [1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 2; 2; 2; 2; 2; 3; 3; 3; 3; 3; 3; 3; 3; 3; 3; 3; 3; 3; 3; 3; 3; 3; 3; 3; 3]
after implementing some sort of code I want to get 20 ones for index 1 through 19, then 20 twos for index 20 through 39, then 20 threes for index 40 through 59.
array1 = [1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 2; 2; 2; 2; 2; 2; 2; 2; 2; 2; 2; 2; 2; 2; 2; 2; 2; 2; 2; 2; 3; 3; 3; 3; 3; 3; 3; 3; 3; 3; 3; 3; 3; 3; 3; 3; 3; 3; 3; 3]
hosein Javan
hosein Javan am 12 Aug. 2020
Bearbeitet: hosein Javan am 12 Aug. 2020
is this what you expect? it will embed 15 twos after the 5th two and shifts the rest down.
reslut = [array1(1:24);repelem(2,15).';arrya1(25:44)]
yes, however I would like to do it in reverse taking into account @Walter Roberson's comment
I tried the following but it did not work:
result = [array1(end:25);repelem(2,15).';array1(24:1)]
which gave me the following array result: an array consisting of 15 twos only
result = [2; 2; 2; 2; 2; 2; 2; 2; 2; 2; 2; 2; 2; 2; 2]
hosein Javan
hosein Javan am 12 Aug. 2020
that means array1 is empty vector, are you sure you have run the program and array1 is created in a correct way?
gummiyummi
gummiyummi am 12 Aug. 2020
Bearbeitet: gummiyummi am 12 Aug. 2020
I changed the code to the following:
result = [array1(end:-1:25);repelem(2,15).';array1(24:-1:1)]
now it works...
will try it out with an if loop for multiple row additions and hopefully it will still work

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Walter Roberson
Walter Roberson am 12 Aug. 2020

0 Stimmen

Since adding 15 indexes to 401 will shift the next index for which I add values by 15
There is a fairly simple way to get around that problem:
Work backwards. Start from the end and go back towards the beginning.
That way, every index that you have not yet worked on is still valid, because it refers relative to the beginning of the array and you have not changed from the beginning of the array to your current point.

1 Kommentar

when using a for loop for descending order, after the for loop is finished only the final i has been included in the changed array.
my for loop runs 4 times... but in my final array only for i=4 have the rows been added. could you help?
my code:
array2 = [1117,15,11 ; 516,8,4 ; 401,2,15]
%[index to start adding rows from, cell value, no. of rows to add]
for i=1:size(array2,1)
result = [array1(end:-1:array2(i,1)) ; repelem(array2(i,2),array2(i,3)).' ; array1(array2(i,1)-1:-1:1)]
end

Melden Sie sich an, um zu kommentieren.

Kategorien

Gefragt:

am 11 Aug. 2020

Kommentiert:

am 12 Aug. 2020

Community Treasure Hunt

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

Start Hunting!

Translated by