matrix result storing problem

2 次查看(过去 30 天)
Sayanta
Sayanta 2011-4-8
Dear Experts,
Could please help me in this matrix result storing problem code
Here is one the prob I'm facing in matlab I want store all the 200 steps computation in V matrix . But when it computes it displays all 200 but only stores the last value in v matrix in matlab work space. I want store all the 200 values in V matrix.
here is the code
v = zeros(1,size(transFreq,1));
% which all the coloums with zeros and no of columns of transfreq = No of column v matrix ; transfreq is 5by5 Matrix so V matrix becomes : 0 0 0 0 0%
v(1,1)=1
%And v = zeros(1,size(transFreq,1)) makes a matrix (1*5), v(1,5)=1 gives v = 0 0 0 0 1%
for j = 1:200 % gives number of iteration v = vP v=v*transFreq end
result v = 0.2628 0.2872 0.3028 0.1472 ( only this value is stored in matlab workspace in V )
many thanks in advance
Sayanta

回答(3 个)

Friedrich
Friedrich 2011-4-8
Hi Sayanta,
this is because you are overwriting the value of v in every step of the loop. If you want to store every value you have to store them explicit, e.g.
for j=1:100
v = v*P*v;
store(j) = v;
end
Please don't forget to preallocate memory for the store value first. You can also store all the values in v but you have to modify the loop a little bit
for j=1:100
v(j,:) = = v(j-1,:) * P * v(j-1,:);
end
This code will not work. You have to modify the dimensions and indexing to your own needs.
I hope I could help,
Friedrich

Robert Cumming
Robert Cumming 2011-4-8
it looks like you are overwriting your variable v in your loop. I dont really follow your code in the example above, and not knowing what your variables are, e.g. vP or trasFeq.
Anyway here is some code to show you how to populate an array in a loop:
transFreq = rand(200,1);
v = zeros(4,size(transFreq,1));% pre allocate your matrix
for j = 1:200
temp = rand(4,1);%vP??
v(:,j)=transFreq(j).*temp;
end
You might be able to vectorise your depending on your variables.

Mohammad
Mohammad 2011-4-9
Hello Sayanta For storing values of v , as matrixes , you can use a cell structure or a matrix , that cell is easier.
cell: for j=1:100 v = v*P*v; store{j} = v; end
martix: for j=1:100 v = v*P*v; store(j,:) = v; end
Be succesful

类别

Help CenterFile Exchange 中查找有关 Large Files and Big Data 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by