Adding a smaller array to a larger array within a loop

7 次查看(过去 30 天)
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

回答(1 个)

Navdha Agarwal
Navdha Agarwal 2019-6-21
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)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by