Adding a smaller array to a larger array within a loop

16 ビュー (過去 30 日間)
Liam McWilliams
Liam McWilliams 2019 年 2 月 2 日
回答済み: Navdha Agarwal 2019 年 6 月 21 日
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 Center および File ExchangeMatrix Indexing についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by