Query in applying If command

조회 수: 1 (최근 30일)
Prakhar Modi
Prakhar Modi 2019년 9월 25일
댓글: Prakhar Modi 2019년 9월 26일
Hello everyone,
First i'll write the part of the code than I'll ask the doubt.
O=1:25;
n=randn(100,1);
for j=1:10
for i=1:length(n)
G(j)= a+ b*n(i);
if (abs(G(j)-O(:))<1)
n(i)=[ ]
break
end
end
end
SO here you can see I need to generate G on the basis of a and b and n(random number generated). The value of G(j) shall be near to O for which I have used the if command.
Now if I want to generate 10 values of G, so My doubt are:
1) Is [abs(G(j)-O(:))<1 ] correct? I want that [abs(G(j)-O(:))<1] with any one of the 25 values of O. Then before going to next iteration of j, I am removing the n(i) which is used to generate G(j) in last iteration and similarly i want to remove the value of O which is used in last iteration. So what I want is that when I run the next iteration there will be one less value in O and n after every iteration which is used in previous iteration.
2) How to remove the particular value of O which is used in previous iteration before going to next iteration.
  댓글 수: 2
KALYAN ACHARJYA
KALYAN ACHARJYA 2019년 9월 25일
"a" is undefined??
G(j)= a+ b*n(i);
Prakhar Modi
Prakhar Modi 2019년 9월 25일
a has some value let say a= 7.5.
I just need to know how to check the if condition for all observed data but whichever value the particular iteration is using should be removed before next iteration.

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

답변 (1개)

Jan
Jan 2019년 9월 25일
if (abs(G(j)-O(:))<1)
The if command needs a scalar condition. Therefore Matlab inserts an all() to evaluate the vector G(j)-O(:) < 1. Are you sure, that this is wanted. Then write it explicitly to avoid confusions.
This canniot work: n(i)=[ ]. After you have removed the element n(i), the vector n is shorter. Then you cannot access all elements until 100.
What about this:
O = 1:25;
a = 7.5;
for k = 1:10
match = false;
while ~match
x = a + b * randn;
n = find(abs(x - O) < 1);
if ~isempty(n)
match = true;
O(n) = [];
end
end
G(k) = x;
end
This is inefficient. There is no guarantee, that e.g. the last value of O matchs a random number in a finite amout of time. Maybe it is better, if you explain the problem you want to solve.
  댓글 수: 1
Prakhar Modi
Prakhar Modi 2019년 9월 26일
Thanks for the reply.
But n(i)=[ ] is working and I need to remove it for the next iteration and I want that in next iteration it should access 99 elements. And similarly I need that it should remove 1 element out of 25 element of O which is used, so that in next iteration it will only have remaining 24 elements.
can you suggest me that how the code will check the if condition for all elements of O one by one and whenever it satisfies it removes that element of O before next iteration.

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

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

태그

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by