Labfans是一个针对大学生、工程师和科研工作者的技术社区。 论坛首页 | 联系我们(Contact Us)
MATLAB爱好者论坛-LabFans.com
返回   MATLAB爱好者论坛-LabFans.com > 其它 > 资料存档
资料存档 资料存档
回复
 
主题工具 显示模式
旧 2019-12-14, 20:13   #1
poster
高级会员
 
注册日期: 2019-11-21
帖子: 3,006
声望力: 66
poster 正向着好的方向发展
帖子 使用unique()在MATLAB中重构向量

假设A是一个200项的单元格数组,其中包含4个不同的字符串(每个字符串都有50个重复)。 B是一个带有一些整数的200项向量。

我使用[cellNos cellStartInd enumCells ] = unique(A)并获得这项A等于唯一的字符串中的一个(enumCells是包含排序的枚举的字符串整数1-4,阵列)。

我想使用此信息从B创建一个4x50的值矩阵,以便每一列都具有特定唯一字符串的值。换句话说,我想将B重塑成一个矩阵,其中矩阵根据A每个唯一字符串排列。



回答:

假设您已经知道将要重复多少次,并且所有字符串都以相同的频率重复,您可以执行以下操作:

%# sort to find where the entries occur (remember: sort does stable sorting) [~,sortIdx] = sort(enumCells); %# preassign the output to 50-by-4 for easy linear indexing newB = zeros(50,4); %# fill in values from B: first the 50 ones, then the 50 2's etc newB(:) = B(sortIdx); %# transpose to get a 4-by-50 array newB = newB'; 或者,以更紧凑的方式(感谢@Rich C)

[~,sortIdx] = sort(enumCells); newB = reshape(B(sortIdx),50,4)';

更多&回答...
poster 当前离线   回复时引用此帖
回复


发帖规则
不可以发表新主题
不可以发表回复
不可以上传附件
不可以编辑自己的帖子

启用 BB 代码
论坛禁用 表情符号
论坛启用 [IMG] 代码
论坛启用 HTML 代码



所有时间均为北京时间。现在的时间是 19:42


Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.