I want to know how to assing variable value depending on the value it self. Explained in detail below.

조회 수: 3 (최근 30일)
Consider I=[2,3,5];
now depending on the values of I i want to assign other variables as
r2=2, r3=3, r5=5.
If the elements in I are huge, how do I assign the index in the variable r in a simple for loop ??
  댓글 수: 1
Stephen23
Stephen23 2019년 1월 7일
"now depending on the values of I i want to assign other variables as r2=2, r3=3, r5=5."
That is not a good approach to writing code. Read this to know why:
It would be much simpler and more efficient to just use indexing.

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

채택된 답변

Stephen23
Stephen23 2019년 1월 7일
편집: Stephen23 2019년 1월 7일
This is MATLAB, so just keep it simple:
>> x = [2,3,5];
>> r = zeros(1,max(x));
>> r(x) = x
r =
0 2 3 0 5

추가 답변 (1개)

Ganesan S
Ganesan S 2019년 1월 7일
Dear Sir,
Why do you want to assign a variable (I) to a different variable (r)? I(i) itself is will do the work.
Try this:
for i=1:length(I)%if I is either row vector or column vector
r(i)=I(i);
end

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by