Azzera filtri
Azzera filtri

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

4 visualizzazioni (ultimi 30 giorni)
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

Risposte (2)

David Sanchez
David Sanchez il 23 Mag 2013
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 Commenti
David Sanchez
David Sanchez il 23 Mag 2013
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 il 24 Mag 2013
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...

Accedi per commentare.


Matt J
Matt J il 23 Mag 2013
Modificato: Matt J il 23 Mag 2013
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 Commenti
Ben
Ben il 24 Mag 2013
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 il 24 Mag 2013
Modificato: Matt J il 24 Mag 2013
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.

Accedi per commentare.

Categorie

Scopri di più su Sparse Matrices in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by