フィルターのクリア

Can't Assign the Values of a changing vectors to the new ones

2 ビュー (過去 30 日間)
farzad
farzad 2015 年 4 月 18 日
編集済み: farzad 2015 年 4 月 19 日
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 件のコメント
farzad
farzad 2015 年 4 月 18 日
yes Z changes in each iteration ,and every time the z(2) has a new quantity , you are right , but all of my code is too much to put it here , that 's the problem
per isakson
per isakson 2015 年 4 月 18 日
編集済み: per isakson 2015 年 4 月 18 日
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.

サインインしてコメントする。

採用された回答

Geoff Hayes
Geoff Hayes 2015 年 4 月 18 日
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 件のコメント
farzad
farzad 2015 年 4 月 18 日
n is nothing but a simple counter , and as I wrote above it starts from and increase by step of size 1
Geoff Hayes
Geoff Hayes 2015 年 4 月 19 日
編集済み: Geoff Hayes 2015 年 4 月 19 日
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.

サインインしてコメントする。

その他の回答 (1 件)

Image Analyst
Image Analyst 2015 年 4 月 19 日
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.

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by