필터 지우기
필터 지우기

How to do counter in matlab?

조회 수: 10 (최근 30일)
Hello Matlab
Hello Matlab 2015년 4월 21일
편집: Stephen23 2019년 6월 25일
I want to do just like the following Matlab, it returns error, how to fix it so Matlab can take it without errors?
load M.dat
t = 600;
q(1)=[1;0;0;0;0;0;0;0;0;0];
for i=1:t-1,
q(i+1) = q(i)*M;
end
I want to have qt in the end. q(1) is a matrix called q1, and I want q2 which is q(i+1) to be M*q1 , and q3 to be M*q2 and visa versa until we reach qt
  댓글 수: 4
James Tursa
James Tursa 2015년 4월 21일
Then q(i)*M is scalar*matrix, which is a matrix. How do you expect to store this into q(i+1), which is a scalar? You need to examine your equations and correct them to something that makes sense.
Hello Matlab
Hello Matlab 2015년 4월 21일
I edit the question, I hope it makes sense now!!!

댓글을 달려면 로그인하십시오.

답변 (3개)

Image Analyst
Image Analyst 2015년 4월 21일
You can't assign a 10-element column vector to a single element of q. Only one element can go into the first element of q, not 10.
  댓글 수: 2
Hello Matlab
Hello Matlab 2015년 4월 21일
This is not to determine the elements, this is a matrix called q0, and I want q1 to be M*q0 , and q2 to be M*q1 and visa versa until we reach qt.
Image Analyst
Image Analyst 2015년 4월 21일
Try this:
M = 100 % Whatever...
t = 600;
q = zeros(1, t);
q(1)= 1;
for i=1:t-1,
q(i+1) = q(i)*M;
end

댓글을 달려면 로그인하십시오.


James Tursa
James Tursa 2015년 4월 21일
I am still guessing here. Maybe this is what you want? (I am assuming M is square)
load M.dat
t = 600;
q = zeros(size(M,1),t);
q(1,1) = 1;
for k=2:t,
q(:,k) = M * q(:,k-1);
end
Each column of q is an intermediate answer, with q(:,t) being your final answer.
If you don't need all the intermediate answers, then simply
load M.dat
t = 600;
q1 = zeros(size(M,1),1);
q1(1) = 1;
qt = M^(t-1) * q1;

ALEX
ALEX 2015년 4월 21일
Hello,
Play around with this, let me know if you have problems.
clear all M = 1.1; t = 600; q1 = zeros(10,1); q1(1)= 1;
val = q1*M;
for i=1:t-1
eval(['q' num2str(i+1) ' = val ']);
val = val*M;
end
Peace
  댓글 수: 2
James Tursa
James Tursa 2015년 4월 22일
Read this link to see why creating hundreds of variables q1, q2, ... etc in a loop is generally very bad programming practice:
Stephen23
Stephen23 2015년 4월 22일
편집: Stephen23 2019년 6월 25일

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by