Can't Assign the Values of a changing vectors to the new ones
Show older comments
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 Comments
farzad
on 18 Apr 2015
farzad
on 18 Apr 2015
John D'Errico
on 18 Apr 2015
Edited: John D'Errico
on 18 Apr 2015
No. digits has NOTHING to do with whatever is your problem. But so far, you have shown too little code to have any idea what you did wrong.
farzad
on 18 Apr 2015
Geoff Hayes
on 18 Apr 2015
@farzad - you need to provide all of the code that you have written in order for us to determine what the problem is. What is a, what does a iterate over, what is z? If I were to run your code as above, then every element of weight would be identical because you always assign
weight(n) = z(2);
Unless z changes on each iteration of the for loop?
farzad
on 18 Apr 2015
per isakson
on 18 Apr 2015
Edited: per isakson
on 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.
Accepted Answer
More Answers (1)
Image Analyst
on 19 Apr 2015
1 vote
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.
Categories
Find more on Multidimensional Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!