create a matrix in a function

1 回表示 (過去 30 日間)
Tony Montgomery
Tony Montgomery 2014 年 9 月 6 日
コメント済み: Star Strider 2014 年 9 月 6 日
I have to create a function called ssolve. in the function i need to create an array that can accept 3 variables a, b, n. these variables need to be placed in a matrix. example a needs to be in row 1 column 2, and b needs to be in row 20 columns 1-19. ones must be diagonal, and the rest are zero. in another array, (1by20) i need to have the first 16 to be 1-16, the 17th must be n-2, 18th must be 0, and the last must be n. how can I put that in, ive tried
c=ones(1,20);
m=diag(c);
m(1,2)=a;
m(20,:)=[b(1,19) 1];
but i get an error, what am i doing wrong?

採用された回答

Star Strider
Star Strider 2014 年 9 月 6 日
You’re close!
Change the m(20,:) assignment to:
m(20,:)=[b*ones(1,19) 1];
MATLAB creates automatically incremented vectors using the colon ‘:’ operator. To create a vector that goes from 1 to 7:
v = 1:7;
If you wanted to change the 5th element of v here to 100:
v(5) = 100;
Display v to see the result by just typing v in the Command Window.
That is not the exact answer to the second part of your question, but it will get you started.
  2 件のコメント
Tony Montgomery
Tony Montgomery 2014 年 9 月 6 日
THANK YOU THANK YOU THANK YOU. You really are like my best friend right now. I'm sorry to keep asking questions but I'm new at this and not very good at it so thank you so much again!!
Star Strider
Star Strider 2014 年 9 月 6 日
My pleasure!
Ask away!
We’ve all been there.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by