# How to do counter in matlab?

96 views (last 30 days)
Hello Matlab on 21 Apr 2015
Edited: Stephen23 on 25 Jun 2019
I want to do just like the following Matlab, it returns error, how to fix it so Matlab can take it without errors?
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
Hello Matlab on 21 Apr 2015
I edit the question, I hope it makes sense now!!!

Image Analyst on 21 Apr 2015
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 CommentsShowHide 1 older comment
Image Analyst on 21 Apr 2015
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 on 21 Apr 2015
I am still guessing here. Maybe this is what you want? (I am assuming M is square)
t = 600;
q = zeros(size(M,1),t);
q(1,1) = 1;
for k=2:t,
q(:,k) = M * q(:,k-1);
end
If you don't need all the intermediate answers, then simply
t = 600;
q1 = zeros(size(M,1),1);
q1(1) = 1;
qt = M^(t-1) * q1;

ALEX on 21 Apr 2015
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 CommentsShowHide 1 older comment
Stephen23 on 22 Apr 2015
Edited: Stephen23 on 25 Jun 2019

### Categories

Find more on Matrix Indexing 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!