Arrays 在Matlab中求元素w.r.t原矩阵整形后的位置
我有一个1*262144的矩阵,我已经把它重塑成了512*512的矩阵。现在,我需要第二个矩阵中的某些元素,并想知道它们在原始行矩阵中的位置。比如说,我需要一个元素,它在重塑后的矩阵中位于(256,4)。我怎样才能知道这个元素在原始的纯行矩阵中的位置Arrays 在Matlab中求元素w.r.t原矩阵整形后的位置,arrays,matlab,matrix,reshape,Arrays,Matlab,Matrix,Reshape,我有一个1*262144的矩阵,我已经把它重塑成了512*512的矩阵。现在,我需要第二个矩阵中的某些元素,并想知道它们在原始行矩阵中的位置。比如说,我需要一个元素,它在重塑后的矩阵中位于(256,4)。我怎样才能知道这个元素在原始的纯行矩阵中的位置 matri_working_now = C(1,:); matrix_working_now = reshape(matri_working_now,512,512); [nrows,ncols] = size(matrix_st
matri_working_now = C(1,:);
matrix_working_now = reshape(matri_working_now,512,512);
[nrows,ncols] = size(matrix_stables); %matrix_stables is a matrix over which I am looping over which contains the locations of the desired elements as per the reshaped matrix. this itself is a 30839*2 matrix
for row = 1:nrows
for col = 1:ncols
%sub2ind(size(matrix_working_now),row,col)
%fprintf('iteration is equal to %6.2f.\n',row,col);
[rowss colum] = ind2sub(size(matri_working_now),sub2ind(size(matrix_working_now),matrix_stable(row),matrix_stable(col))); % i am accessing the elements of matrix_stables which provide me the row and column numbers;
end
end
有什么建议/想法吗
谢谢 由于原始矩阵是一个向量,您只需使用: 通常,如果原始矩阵不一定是向量,则需要第二步: 例如:
我得到了“超出范围的下标错误”;我正在重新发布我的完整代码以便更好地查看。请查看原始帖子中编辑的代码。谢谢@KashifNawaz在您的
sub2ind
行中,我想您已经互换了尺寸。与我的回答相比,同样的错误再次出现!矩阵是我的原始矩阵;矩阵_uu是我的重塑矩阵。否则,我没有明白你的意思。请看上面编辑的代码。现在,对于类型为“double”的输入参数,得到了错误的未定义函数“matrix_stable”。我想我必须使用mat2something进行此更改,您认为是哪一个?
col = sub2ind(size(reshapedMatrix), 256,4);
[row col] = ind2sub(size(originalMatrix), sub2ind(size(reshapedMatrix), 256,4));
>> originalMatrix = (1:10).^2
originalMatrix =
1 4 9 16 25 36 49 64 81 100
>> reshapedMatrix = reshape(originalMatrix, 2,5)
reshapedMatrix =
1 9 25 49 81
4 16 36 64 100
>> reshapedMatrix(2,3)
ans =
36
>> [row col] = ind2sub(size(originalMatrix), sub2ind(size(reshapedMatrix), 2,3))
row =
1
col =
6
>> originalMatrix(row,col)
ans =
36