本文介绍了在Matlab中对列进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2列通过使用textscan导入的数据.数据如下所示,其中 U 未检测到,而 D 检测到

I have 2 columns of data imported from using textscan. The data looks like this where U is undetect and D is detect

mydata=

.51 U
.57 D
.48 U
.47 D

 my data = [4x1 double]    [4x1 char]

我想按第一列对数据进行排序,因此数据看起来像这样

I want to sort the data by the first column and so the data would look like this

.47  D
.48  U
.51  U
.57  D

我想保留单元格结构,以便以下分配逻辑值的命令仍然成立:

I would like to preserve the cell structure so that the following command to assign logical value still hold true:

c = zeros(size(mydata,1),1); % preallocate empty matrix

c = mydata{2} == 'U';
for i = 1:size(mydata,1)
      curValue = mydata{i,2};
      data{i,3} =  ~isempty(curValue) && ischar(curValue) && strcmp(curValue ,'U');
end

我阅读了有关排序行的信息,但该函数用于对仅包含数字的矩阵进行排序.

I read about sortrows but the function is used to sort matrix containing just numbers.

有人能解决用数字和字符混合的数组排序的问题吗?

Does anyone have a solution for sorting arrays with a mixture of numbers and characters.

推荐答案

您可以 SORT 一个向量并将排序索引应用于另一个向量.

You can SORT by one vector and apply the sorting index to another vector.

[mydata{1},idx] = sort(mydata{1});
mydata{2} =  mydata{2}(idx);

这篇关于在Matlab中对列进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 02:57