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

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

I think once I inserted a command like digits or something like this , maybe that's the reason ??
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.
well I don't know what more you might require , immagine a vector z as I mentioned with ANY non zero arguments , I simply , in the loop , want to add the second row of this vector to a new one , It accepts till the second element as you see ,the second one is : 0.3788 0.3268
the third one had to be : 0.3788 0.3268 0.3356
and the forth one : 0.3788 0.3268 0.3356 0.3452
but it's in the way you see in the question, this is the question !
@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?
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
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.

Sign in to comment.

 Accepted Answer

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 Comments

z is a vector that uses a saved neural network data to create this 3 x 1 vector , when I just type z(2) , it writes the right number , but when I write weight(n) = z(2); it just doesn't add the right number to the code ,
I think this does not have anything to do with the main code , it can happen anywhere
What is n? Are you sure that it is a scalar? Are there any other lines in the code that assign values to weight? Why can't you attach your code so that we can have a look at it?
sure here it is
close all;
clear all;
clc;
% Applied Transverse Loading
q0=1000;
%
% Plate dimensions
n is nothing but a simple counter , and as I wrote above it starts from and increase by step of size 1
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.

Sign in to comment.

Categories

Tags

Asked:

on 18 Apr 2015

Edited:

on 19 Apr 2015

Community Treasure Hunt

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

Start Hunting!