필터 지우기
필터 지우기

How can I access the previous index of a vector?

조회 수: 3 (최근 30일)
Khalid Khawaja
Khalid Khawaja 2017년 4월 24일
댓글: Khalid Khawaja 2017년 4월 24일
Hello friends,
I am trying to write a code to generate random vectors. Some of the variables are integer whereas a few are real numbers. Following is the code:
xL=[1 500 1 500];
xU=[37 1400 37 1400];
Xint=[1 3];
nvar=4;
for xV=1:nvar
if Xint(xV)==xV
InPoP(:,xV) = randi([xL(xV) xU(xV)],Popu_size,1);
else
InPoP(:,xV) = (xU(xV)-xL(xV)).*rand(Popu_size,1) + xL(xV);
end
end
Xint vector shows that first and third elements should be generated as integer. The problem is when the loop moves away from x=2. It cannot access the elements of Xint to know the number. I would be grateful if someone can share the solution of the problem.
Regards
  댓글 수: 2
KSSV
KSSV 2017년 4월 24일
You have in Xint only two numbers and you are trying to access four numbers out of it. Make Xint to have four numbers i.e include few more numbers.
Khalid Khawaja
Khalid Khawaja 2017년 4월 24일
How can I write a program to generate Xint variables integer using iterative schemes?

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

채택된 답변

Walter Roberson
Walter Roberson 2017년 4월 24일
Change
if Xint(xV)==xV
to
if ismember(xV, Xint)
  댓글 수: 1
Khalid Khawaja
Khalid Khawaja 2017년 4월 24일
Thank you very much for the answer. I was looking for exactly the same thing. Regards

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

추가 답변 (1개)

Roger Stafford
Roger Stafford 2017년 4월 24일
You don’t appear to use ‘Xint’ for any other purpose than deciding whether to use integers or reals. Therefore I suggest you change it to:
Xint = [1 0 1 0];
for xV=1:nvar
if Xint(xV) == 1
% Use integers
else
% Use reals
end
  댓글 수: 1
Khalid Khawaja
Khalid Khawaja 2017년 4월 24일
Thank you for your answer. It is also a good solution. Actually, I need Xint values for further operations. Changing the values to binary will need many changes in my code. Regards

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

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by