Appending vectors in a forloop dependent on iteration.
3 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
For a m × m matrix A and a fixed vector v ∈ Rn, I am asked to compute the time it takes to compute Av in Matlab, and plot such time for m = 2j, j = 1, 2, . . . , 12. So pretty much the size of the matrix increases in multiples of 2 and as a result I would have to change the size of the vector to make sure dimensions match. Here is what I have so far, however I do not know the syntax for adding 2^(i-1) entries to the vector v for each iteration. (I did the math and this should allow the matrices to multiply without an error. Here is the code I have so far:
tl;dr I need to append 2^(i-1) entries to my vector v after each iteration. I also am not sure if the way I am plotting it makes sense.
v = [1, 2]' % I fix a 2x1 vector.
A = randint(2,2,[0,100]) % I generate a random 2x2 matrix.
time = zeros(1,12); % I create a 1x12 vector.
for i=1:12
if i==1 % When i=1, I could just multiply the
tic; % 2x2 matrix and the 2x1 vector.
w = A*v;
t1 = toc;
time = [time toc] % Add the cpu time for i=1.
else
tic;
A = randint(2^(i), 2^(i), [0,100]); %This will allow me to generate
%4x4 matrices, 8x8 matrices
%etc.
%How do I add 2^(i-1) entries to v that are random? I need to do
%this in order to make the dimensions match when I multiply the
%vector and the matrix together.
w = A*v;
time2 = toc;
time = [time time2] % Add the cpu time for i=2....12.
end
plot([1:12], time) % Let the x-variable be the value of i
%and let the cpu time be on the y-axis.
The mathworks community is great and any and all help will be greatly appreciated. Sincerely, -Andrew.
0 Commenti
Risposte (0)
Vedere anche
Categorie
Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!