필터 지우기
필터 지우기

Array comparision and replacement

조회 수: 1 (최근 30일)
Kevin Nieto
Kevin Nieto 2019년 2월 14일
편집: Yasasvi Harish Kumar 2019년 2월 26일
I have 2*one dimention arrays of the same length called A and B;
I have a third one with 6 spaces called C;
I want to compare each element starting from the seventh element of the A array to the end of that array, with all the elements already saved in . (The length of A and B is random but always the same length)
If that element of A that I'm comparing is greater than one of the six saved on C, I want to replace that space on C with the sum of A and B from the position that (won) was greater than a C element. I only have to find if there's ONE that is lower than the current A element.(Not two or more, just one).So Inmediatly increase a counter (of replacements) in only one unit and go to the next element of the array A and repeat the process until I end the A array.
If the number saved on A isn't greater than any of the numbers saved on C Just skip to the next element of A. Until I finish the array.
Hope that's clear enough and if its not let me know
Im new to MATLAB coming form c++ and tried to do it there with no success. Just to give you an idea. Thank you in advance
for(i=6;i<11;i++)
while (flag!=1){
for(m=0;m<6;m++)
if (A[i]>C[m]){
C[m]=A[i]+B[i];
pe=pe+1;
flag=1;
}
}
Another attempt of doing it on matlab with no success. Just take the idea
while flag~=1
for i=7:length(A)
for m=1:length(C)
if A(i)>C(m)
C(m)=A(i)+B(i)
flag=1
end
end
end
  댓글 수: 2
KSSV
KSSV 2019년 2월 14일
An example will help us to help you.
Kevin Nieto
Kevin Nieto 2019년 2월 14일
A={12,62,3,45,53,7,32,34,3,42,37,36}
B={11,64,3,5,6,30,14,1,7,32,16,25}
C={33,126,6,50,59,37}
Take the seventh from A: 32
compare with the six I have on C. it is bigger than 6, so Ill replace 6 with 46 (the sum of 32+the seventh number of B(14)), increase my counter to 1 and skip to the next element of A because I found one number on C lower than the one im comparing. If i didn't just skip to the next number, dont increase the counter and repeat the process with the eight one. At the end i want to know how many numbers I replaced

댓글을 달려면 로그인하십시오.

채택된 답변

Yasasvi Harish Kumar
Yasasvi Harish Kumar 2019년 2월 14일
편집: Yasasvi Harish Kumar 2019년 2월 26일
Hey,
From what I understand, for each element of A that is compared with C, there is a maximum occurance of one replacement. Your objective is to find the total number of replacements occured in C. The following code should be able to help you with the variable counter containing the number of replaced elements.
counter = 0;
for i=7:length(A)
flag = 0;
for m=1:length(C)
if A(i)>C(m) && flag~=1
C(m)=A(i)+B(i);
flag=1;
counter = counter +1;
end
end
end
Regards
  댓글 수: 1
Kevin Nieto
Kevin Nieto 2019년 2월 14일
Infinite thanks!! Have a nice Day/Night
Greetings from México

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by