Variable

1 次查看(过去 30 天)
developer
developer 2011-9-7
Hello, If i have
X=rand(20,1)
Y=rand(20,1)
then i want to do
p1=[x(1) y(1)]
p2=[x(2) y(2)]
. . .
. . .
. . .
p20=[x(20) y(20)]
How can i intialize these p1 to p20 values with the help of loop instead of intiallizing manually?
The varaible needs to be updates as p1...p20.
Finally with structure P has terms p1...p20, with p1...p20 have their values from X and Y
Thanks in advance

采纳的回答

Andrei Bobrov
Andrei Bobrov 2011-9-7
variant use cell array
X=rand(20,1)
Y=rand(20,1)
p = mat2cell([X Y],ones(20,1),2)
bad version
for j1 = 1:size(X,1)
jc = num2str(j1);
eval(['p' jc '= [X(' jc '), Y(' jc ')]']);
end
So do not ever! Use the better 'p{1}' of the first variant instead of 'p1'
  12 个评论
Oleg Komarov
Oleg Komarov 2011-9-8
@developer: structs and cells are fine.
Fangjun Jiang
Fangjun Jiang 2011-9-8
Good one, Jan! My mind was stuck with the struct().

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Structures 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by