matrix result storing problem
조회 수: 2 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
답변 (3개)
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
댓글 수: 0
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.
댓글 수: 0
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
댓글 수: 0
참고 항목
카테고리
Help Center 및 File 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!