How to insert an array into a matrix?

9 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 Number games 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by