Adding a smaller array to a larger array within a loop

9 views (last 30 days)
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

Answers (1)

Navdha Agarwal
Navdha Agarwal on 21 Jun 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)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!