필터 지우기
필터 지우기

using values in a matrix as index for other matrix

조회 수: 4 (최근 30일)
Preethi
Preethi 2014년 12월 22일
편집: Stephen23 2014년 12월 22일
Hello,
I am having matrix G of size 30 x 30 and other matrix L of size m x 2 where m <30, I want to update the values in G based on the values in L
for example L=[1,2;3,4;5,6;7,8;9,10]
L =
1 2
3 4
5 6
7 8
9 10
I want to update values of G(1,2), G(3,4).... i tried sub2ind(), but i am getting "out of range subscript error" how do i resolve this?
Thanking you in advance

채택된 답변

Stephen23
Stephen23 2014년 12월 22일
편집: Stephen23 2014년 12월 22일
sub2ind does exactly what you need:
L=[1,2;3,4;5,6;7,8;9,10];
G = reshape(1:30*30,30,[]);
X = sub2ind(size(G),L(:,1),L(:,2));
G(X)
That error would indicate that your matrix is not really the size that you think it is, and the indices are trying to reference values outside size of the matrix... you should use the size command to check your matrix G, and also try this:
numel(G)
This value must be <= to the maximum index values given in our new variable X, otherwise you will get this "out of range" error.

추가 답변 (1개)

Image Analyst
Image Analyst 2014년 12월 22일
Try this:
L=[1,2;3,4;5,6;7,8;9,10]
% The first column of L is the row to change.
% The second column of L is the column to change.
% Create linear indexes.
linearIndexes = sub2ind(size(G), L(:,1), L(:, 2))
% Let's say G starts out as all 99's.
G = 99 * ones(30);
% Now let's change the specified (row, column) locations to 1
G(linearIndexes) = 1

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by