How do I stoop overwriting the variable in a "for" loop?

4 次查看(过去 30 天)
Hi, I'm using the following code:
for i=1:nbs.NBS.n
global nbs;
adj=nbs.NBS.con_mat{i}+nbs.NBS.con_mat{i};
adj=full(adj)
end
nbs.NBS.n = 21 so it iterates 21 times through it, so far so good. However every time it overwrites the variable "adj". What I would like it to do is create adj_1 to adj_21, How can I do that?
I tried typing adj{i}... and adj.(i).... both don't work. Any ideas please?
Thanks

回答(2 个)

David Sanchez
David Sanchez 2013-5-23
Try this out:
adj = zeros(nbs.NBS.n,1); % initialize the variable
for i=1:nbs.NBS.n
global nbs;
adj=nbs.NBS.con_mat{i}+nbs.NBS.con_mat{i};
adj(i)=full(adj)
end
  2 个评论
David Sanchez
David Sanchez 2013-5-23
You can try with a cell array as well.
adj = cell(nbs.NBS.n,1); % initialize the variable as a cell array
for i=1:nbs.NBS.n
global nbs;
adj=nbs.NBS.con_mat{i}+nbs.NBS.con_mat{i};
adj{i}=full(adj)
end
Ben
Ben 2013-5-24
Hi, your scripts create the matrix as I want it for the first cell of the nbs:NBS.con_mat cell array but only for the first one and not the remaining 20. I would like to convert every cell of nbs:NBS.con_mat into separate matrices each with a different name starting at adj1 and ending with adj21...

请先登录,再进行评论。


Matt J
Matt J 2013-5-23
编辑:Matt J 2013-5-23
I tried typing adj{i}... and adj.(i).... both don't work. Any ideas please?
Works fine for me. Not sure why you have "global nbs" in the for-loop, though.
nbs.NBS.n=21;
nbs.NBS.con_mat=num2cell(1:21);
for i=1:nbs.NBS.n
global nbs
adj{i}=nbs.NBS.con_mat{i}+nbs.NBS.con_mat{i};
adj{i}=full(adj{i});
end
adj =
Columns 1 through 14
[2] [4] [6] [8] [10] [12] [14] [16] [18] [20] [22] [24] [26] [28]
Columns 15 through 21
[30] [32] [34] [36] [38] [40] [42]
  2 个评论
Ben
Ben 2013-5-24
Hi, nbs.NBS.con_mat is a cell array (1x21) with 116x116 double in each cell. Typically the content of each cell looks like this: val =
(2,58) 1
(57,58) 1
the command:
global nbs; adj=nbs.NBS.con_mat{1}+nbs.NBS.con_mat{1}; adj=full(adj);
converts the 1st cell into a 116x116 double matrix called adj. What I want the loop to do is go through each cell of nbs_NBS.con_mat and create the 116x116 double matrix for each cell as a variable in the workspace. In my version it does that, but it overwrites the existing adj variable. I would like it to create new adj variables for each cell rather than overwriting the existing one... The script itself works but not to the full extent that I want it to work
Matt J
Matt J 2013-5-24
编辑:Matt J 2013-5-24
In my version it does that, but it overwrites the existing adj variable. I would like it to create new adj variables for each cell rather than overwriting the existing one...
Then basically, you're just trying to convert all your matrices from sparse to full form? Anyway, how is that different from having adj{i}, i=1...21 like what the code I gave you produces? Each adj{i} does behave like a separate variable.

请先登录,再进行评论。

类别

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

标签

Community Treasure Hunt

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

Start Hunting!

Translated by