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

1 次查看(过去 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 ??

采纳的回答

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