How to insert an array into a matrix?

3 ビュー (過去 30 日間)
Jordan Conoly
Jordan Conoly 2016 年 3 月 18 日
編集済み: Image Analyst 2016 年 3 月 18 日
Hello,
I'm trying to figure out how to insert an array anywhere into an existing matrix. I currently have the following code:
a=[9 9 9 9 9];
A=zeros(10,10);
%Have the player input the endpoints of the array
x=input('Enter the x coordinate of one end of the array ');
y=input('Enter the y coordinate of one end of the array ');
x1=input('Enter the x coordinate of the other end of the array ');
y1=input('Enter the y coordinate of the other end of the array ');
A(x,y:x1,y1)=a;
However, this only works for if the endpoints are (1,1) and (5,1). If I try other endpoints, an error message pops up. How can I get it to where I can insert this array anywhere in the matrix?
  2 件のコメント
Image Analyst
Image Analyst 2016 年 3 月 18 日
編集済み: Image Analyst 2016 年 3 月 18 日
What do you want to do if parts of the matrix would go outside of the other matrix? Alert the user to pick another location, or just crop off what does not fit? And is this homework (sounds like it)?
Jordan Conoly
Jordan Conoly 2016 年 3 月 18 日
I don't want it to crop off what doesn't fit. And, yes it is for homework

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

回答 (1 件)

Image Analyst
Image Analyst 2016 年 3 月 18 日
編集済み: Image Analyst 2016 年 3 月 18 日
Hint #1:
You don't need to ask for x1 and y1 since it can be computed from x and y and the size of the poorly-named a.
[rows, columns] = size(a);
Hint #2: A is a 2 D array and takes two indexes, not 3 like you tried.
Hint #3: The indexes are not x,y! They are (y, x) because it's (row, column) and y is the row.
Hint #4: use sprintf and uiwait() and helpdlg() to alert the user if the array would go outside the container array
message = sprintf('Your array would go outside....whatever...
uiwait(helpdlg(message));
Give it another try and get back with improved code. Use names like row1, row2, column1, and column2. Like I said, you can get row2 and column2 from adding the size to row1 and column1.

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by