Swapping a range of points in a matrix
이전 댓글 표시
I'm trying to swap rows 6 to 10 in column 1 and rows 1 to 5 in column 2 of my matrix for a task in my computing class. So far this is the best I can manage but something is going wrong, presumably with the square brackets:
r1=zeros(5,6)
r1(2:2:30,1)=1:1:15
r1([1:5,6:10],[2,1])=r1([6:10,1:5],[1,2])
Any help would be much appreciated
댓글 수: 1
dpb
2016년 11월 26일
Matlab doesn't treat arrays of indices as you're trying to use them; see what the expression returns to understand what you're getting (might want to do it for a smaller range first to get fewer values to look at and clarify what's happening).
To do multiple locations like this you'd need to use linear addressing and compute that location from subscripts via the ind2sub|sub2ind pair or write the column indices a single values in separate expressions.
As it is coursework, will leave as the above hints for further exploration... :)
답변 (2개)
Roger Stafford
2016년 11월 26일
Suppose your matrix is called ‘M’.
T = M(6:10,1);
M(6:10,1) = M(1:5,2);
M(1:5,2) = T;
Roger Stafford
2016년 11월 29일
If you prefer one-liners, try this, (again calling your matrix M):
M([6:10,(1:5)+size(M,1)]) = M([(1:5)+size(M,1),6:10]);
카테고리
도움말 센터 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!