Matlab中对矩阵使用矩阵尺寸的索引
2016-03-21 16:04
573 查看
转自stackoverflow: Addressing Matlab matrix with Index-matrix
涉及bsxfun的使用。Addressing Matlab matrix with Index-matrix
I am using the command [A,idx] = sort(A), which sorts the matrix A columnwise, which is what I want. The idx matrix shows how each column of A has been sorted. Now if I try and access B(idx) MATLAB assumes I am referencing B in an absolute manner. This is however not what I want, I want the columns of B sorted with the same scheme A was sorted with.
I am therefore looking for a command that will index a matrix columnwise. Of course I can easily do this with a loop, but it seems like a pretty poor solution. I have also come up with this solution, but it’s not very pretty
Thanks in advance.
Consider the following:
Your solution is not bad, you could improve it a bit by replacing the REPMAT call with BSXFUN:
Another alternative is to convert to linear indices using SUB2IND:
涉及bsxfun的使用。Addressing Matlab matrix with Index-matrix
I am using the command [A,idx] = sort(A), which sorts the matrix A columnwise, which is what I want. The idx matrix shows how each column of A has been sorted. Now if I try and access B(idx) MATLAB assumes I am referencing B in an absolute manner. This is however not what I want, I want the columns of B sorted with the same scheme A was sorted with.
I am therefore looking for a command that will index a matrix columnwise. Of course I can easily do this with a loop, but it seems like a pretty poor solution. I have also come up with this solution, but it’s not very pretty
idx = idx+repmat(0:size(idx,1):size(idx,2)*size(idx,1)-1,size(idx,1),1)
Thanks in advance.
Consider the following:
A = randi([1 10],[5 4]); [AA,idx] = sort(A);
Your solution is not bad, you could improve it a bit by replacing the REPMAT call with BSXFUN:
idx = bsxfun(@plus, idx, 0:size(idx,1):numel(idx)-1); isequal(AA,A(idx))
Another alternative is to convert to linear indices using SUB2IND:
idx = sub2ind(size(idx), idx, repmat(1:size(idx,2),size(idx,1),1)); isequal(AA,A(idx))
相关文章推荐
- Matlab 实现对码功能
- Matlab 日期频次统计
- 《Computational Statistics with Matlab》硬译2
- Gamma函数相关matlab代码
- 【matlab】MATLAB程序调试方法和过程
- matlab简单应用
- matlab 文件夹操作
- MATLAB r2014a 下载+安装+激活
- MATLAB 编程风格指南
- C/C++下调用matlab函数
- matplotlib对ColorBar的处理似乎优于matlab
- 机器学习MatLab实战整理--线性回归
- matlab归一化和反归一化函数——mapminmax
- Matlab 图像处理基本操作
- matlab图像数据转换函数
- Matlab7.0在64位系统下安装的问题…
- 【matlab】:图像获得其边缘
- Matlab中的常用快捷键
- 简述Matlab中size()函数的用法
- 详解Matlab中 sort 函数用法