How to insert an array into a matrix?
조회 수: 2 (최근 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
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)?
답변 (1개)
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.
댓글 수: 0
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!