Adding a smaller array to a larger array within a loop

16 visualizaciones (últimos 30 días)
Liam McWilliams
Liam McWilliams el 2 de Feb. de 2019
Respondida: Navdha Agarwal el 21 de Jun. de 2019
Hi,
I'm trying to set up a loop thats adds a 16x1 array [A] to a 1023x1 array [B] whenever the element value of B is >1e3. The purpose of this is to add the subsequent elements of [A] to the next 15 elements off [B] until there are no more elements of [A] to add. At this point I would like the program to essentially restart so that on the next occasion that B(i)>1e3, the process repeats.
For example:
A=1:16; B=500:1:1523; C=[ ]
Once B=1001, C(1)=1002.
Then C(2)=1004; since B=1002 and A=2.
I know that every element of B after this point is >1e3 but it's just to show what I'm trying to do.
I hope this makes sense and that someone can help me out.
Thanks

Respuestas (1)

Navdha Agarwal
Navdha Agarwal el 21 de Jun. de 2019
Hi Liam,
As I understand from your question, following is the code for what you want to achieve.
From the elements after 1e3 in array B, start adding a number (count,initially 1) and increment it at every step. As soon as the count exceeds 16, reset it back to 1. This is want is in the array A that we want to add.
A=1:16;
B=500:1:1523;
C=[];
count = 1;
for i = 1:length(B)
if(B(i)>1000)
C=[C,B(i)+count];
count = count+1;
if(count > 16)
count = 1;
end
end
end
disp(C)

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by