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 CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by