Can't Assign the Values of a changing vectors to the new ones
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Hi All
I am adding some numbers to a vector called "weight " , but when looping over this , the columns become 0 while they are not as below : z is a 3x1 and I am going to add the second row of z to the vector weight. none of the elements of z are zero , but I don't know why in the new vector I see this behavior
n=1;
for a = ..
weight (n) = z(2)
n=n+1
end
the results is as below :
weight =
0.3192
weight =
0.3788 0.3268
weight =
0.3788 0 0.3356
weight =
0.3788 0 0 0.3452
7 Commenti
per isakson
il 18 Apr 2015
Modificato: per isakson
il 18 Apr 2015
The script
n = 1;
weight = [];
val = ( 1 : 4 );
for ix = 1:4
z(2) = val(ix);
weight(n) = z(2)
n=n+1;
end
outputs
weight =
1
weight =
1 2
weight =
1 2 3
weight =
1 2 3 4
I cannot reproduce the behavior you describe.
Risposta accettata
Geoff Hayes
il 18 Apr 2015
farzad - You understand the difficulty we are facing? You are asking us to determine what is wrong with your code but you aren't showing us the code. How can we suggest the best corrective action that isn't just guesswork?
As the problem occurs on the third iteration of your for loop, then I suggest that you refer to the debugging in MATLAB link and go about stepping through your code (using the debugger) and see why on the third iteration of the for loop the second element of weight is set to zero. Either there is another assignment to weight that you aren't showing us, or the one you have shown us is incorrect.
5 Commenti
Geoff Hayes
il 19 Apr 2015
Modificato: Geoff Hayes
il 19 Apr 2015
farzad - on each iteration of your outer for loop, you do the following
load('net.mat')
which contains 60 variables, one of which is named weight and is assigned the value
0.378797163328186
So on each iteration, you are overwriting the weight variable from the previous iteration with this new 1x1 scalar. Your code then sets some element of this "array" to the new value, but if the index of this new element is greater than two then all elements between the first and the new one will be set to zero. That is why, on the third iteration your weight array has a zero between the first and third elements, on the fourth iteration there are two zeros between the first and fourth elements, etc.
I suggest that either you remove weight from the net.mat file, load it once outside of the outer for loop, remove the call to load this file, or rename the weight variable in your script to something else.
Più risposte (1)
Image Analyst
il 19 Apr 2015
Here's a sure-fire way to solve this: http://blogs.mathworks.com/videos/2012/07/03/debugging-in-matlab/. Guaranteed to fix your problem.
0 Commenti
Vedere anche
Categorie
Scopri di più su Logical in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!