对元胞数组进行排序?
我有一个数字元胞数组,但大部分元胞数组是空的,例如:
x =
[] [6] [] [4] [] [] [] [1]
我有一个匹配的数组 y
y = [1, 3,1,5,7,3,1,5]
我想从元胞数组 x 中获取数字的索引,并使用它们从 y 中获取相应的值。所以 x(2) 与 y(2) 匹配。我使用将 x 转换为数组
x = cell2mat(x);
但问题是它返回
x = [6,4,1]
This 不允许我获得正确的索引,因此我可以对 X 进行排序,然后相应地对 Y 进行排序,以便相同的索引匹配。我尝试使用不适用于元胞数组的排序。
I have a cell array of numbers but the majority of the cell array is empty for example:
x =
[] [6] [] [4] [] [] [] [1]
I have a matching array y
y = [1, 3,1,5,7,3,1,5]
I want to get the index of the numbers from the cell array x and use them to get the corresponding values from y. So x(2) matches with y(2). I convert x to a array using
x = cell2mat(x);
But the problem is that it returns
x = [6,4,1]
This does not allow me to get the correct index so I can then sort X and then sort Y accordingly so the same indices match up. I tried to use sort that does not work for cell arrays.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需使用 y(x) 即可;将从 y 向量返回索引 6、4 和 1。
请注意,返回矩阵的顺序将取决于 x 中索引的顺序;如果您想对
x
进行排序,请在运行y(x)
之前进行。Just use
y(x)
; that will return indices 6, 4, and 1 from they
vector.Note that the order of the returned matrix will depend on the order of the indices in
x
; if you want to sortx
, do it before runningy(x)
.