Appending a column to a matrix in a for loop.

25 ビュー (過去 30 日間)
Andrew You
Andrew You 2014 年 9 月 10 日
編集済み: Julia 2014 年 9 月 10 日
So the task that I am trying to complete is as follows: write a script that starts with a random m vector, and apply the HilbertMatrix (HilbertMatrix(m)) to it N times, saving the result of the n-th multiplication as the n-th column of a matrix V. This is what I have so far:
r = randi([1 100],1,1); %Choose a random number "r" between 1 and 100.
m = randi([1 100],r,1); %Create a vector "m" with dimensions rx1 i.e. a random vector.
H = HilbertMatrix(r); %Create a Hilbert matrix of size rxr.
for i=1:N %For every i, you multiply the vector by the Hilbert matrix.
v = H^(i).*m; %Your result goes to the the 1st column of a new matrix.
V = [V v]; %You continue so that the n-th multiplication of the Hilbert with the vector goes to the nth column.
end
disp(V) %Show the matrix.
I seem to be getting a few errors. I feel though that my logic is there. I am new to MATLAB but have had exeprience with Python. Hopefully, they are syntax errors. Here is my HilbertMatrix function code:
function h = HilbertMatrix(m)
H = zeros(m,m) %Create an empty mxm matrix.
for k=1:m
for j=1:m
H(j,k)=1/(j+k-1) %Definition of the Hilbert Matrix.
H(k,j)=H(j,k) %Entries of a Hilbert Matrix are symmetric along the diagonal.
end
end
end
Any and all help will be greatly appreciated. Thank you!
  1 件のコメント
Andrew You
Andrew You 2014 年 9 月 10 日
My Hilbert function is correct. I ran the function for different n-values and the entries were correct. I believe my script is in need of work.

サインインしてコメントする。

回答 (2 件)

Pierre Benoit
Pierre Benoit 2014 年 9 月 10 日
First, about your function, there is a problem with the output because Matlab is case sensitive. Replace h with H.
Then for your script, ".*" is element-wise multiplication, not matix multiplication. It's simply * Also, you didn't define V before using it, you try to concatenate an undefined vector with v. You could do
V = zeros(r,N);
for i=1:N %For every i, you multiply the vector by the Hilbert matrix.
V(:,i) = H^(i)*m; %Your result goes to the the ist column of a new matrix.
end
Now, this should work. Let me know if I misunderstood something or if there is something wrong.
  1 件のコメント
Julia
Julia 2014 年 9 月 10 日
編集済み: Julia 2014 年 9 月 10 日
Sorry, I only saw your answer after submitting my own!

サインインしてコメントする。


Julia
Julia 2014 年 9 月 10 日
編集済み: Julia 2014 年 9 月 10 日
Hi,
(I am writing this while trying to debug your code ;) )
- Your function HilbertMatrix() returns only a zero-matrix. Your output is "h" but you compute "H".
- You never define "N" (upper limit of the loop).
- Then you get an error for
v = H^(i).*m;
--> delete the point:
v = H^(i)*m;
- And finally you have to initialize V before using it. You write
V = [V v];
But in the first iteration there is no V.
My solution was:
v = H*m;
V = v;
and start the loop with i=2
I hope this yields the right result.

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by