matlab缩小矩阵尺寸,Matlab中对矩阵使用矩阵尺寸的索引

博客讨论了如何在MATLAB中利用索引矩阵对矩阵进行列排序。通过`sort`函数获取排序后的矩阵和对应的索引,然后使用`bsxfun`或`sub2ind`转换索引以实现按相同排序顺序访问另一矩阵的列。文章提供了一个示例并比较了不同方法的实现。
摘要由CSDN通过智能技术生成

转自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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值