I have a 3x1 cell array A which includes:
我有一个3x1细胞阵列a,它包括:
10.2
15.2
7.2
and another 3x1 cell Array B includes:
另一个3x1细胞阵列B包括:
m
l
s
I would like to join these into a new 3x2 cell Array C including:
我想将它们合并到一个新的3x2单元数组C中,包括:
10.2 m
15.2 l
7.2 v
then I want to sort C according to the values of the first column which is supposed to be
然后根据第一列的值对C进行排序
7.2 v
10.2 m
15.2 l
what I did so far to do this is as follows:
到目前为止,我所做的是:
C=A;
C(:,2)=B;
C=sortrows(C,1);
However the result is:
然而结果是:
10.2 m
15.2 l
7.2 v
I think the reason is that it considers the numbers in the first column as characters and the way it sorts them is by looking at the digits of each number one by one from the left.. thus 10.2 is less than 7.2.
I am looking for a way to assign the numbers as numbers to C so when I sort them it considers them as numbers not characters. I tried to use cell2mat when assigning A and B to C but did not work. I have searched the web for this but could not find what I am looking for.
我认为原因是它把第一列的数字看成是字符,它分类的方式是从左边一个一个地看数字。因此,10.2小于7.2。我正在寻找一种方法,将数字作为数字分配给C,所以当我对它们进行排序时,它将它们视为数字而不是字符。我尝试在给C分配A和B时使用cell2mat,但是没有成功。我在网上搜索过,但找不到我要找的东西。
Thank you!
谢谢你!
2 个解决方案
#1
4
This would do what you need:
这可以满足你的需要:
>>
C = cell(numel(A),2);
[Sorted_A, Index_A] = sort(cell2mat(A));
C(:,1) = num2cell(Sorted_A);
C(:,2) = B(Index_A);
For example, for the input as follows:
例如,输入如下:
>>
A = {10.2; 15.2; 7.2};
B = {'m'; 'l'; 'v'};
The result would be:
结果将会是:
>> C
C =
[ 7.2000] 'v'
[10.2000] 'm'
[15.2000] 'l'
EDIT
It seems that in your case, the input array A
is not just a cell array of numbers, but a cell array of strings; i.e., for example,
在你的例子中,输入数组A不仅仅是数字的单元数组,而是字符串的单元数组;即。例如,
>>
A = {'10.2'; '15.2'; '7.2'};
In this case, you need to make the following changes:
在这种情况下,你需要做以下改变:
- Use
str2double(A)
instead ofcell2mat(A)
- 使用str2double(A)代替cell2mat(A)
- Use
strtrim(cellstr(num2str(Sorted_A)))
instead ofnum2cell(Sorted_A);
- 使用strtrim(cellstr(num2str(Sorted_A)))代替num2cell(Sorted_A);
Your final code will now be:
您的最终代码现在将是:
>>
C = cell(numel(A),2);
[Sorted_A, Index_A] = sort(str2double(A));
C(:,1) = strtrim(cellstr(num2str(Sorted_A)));
C(:,2) = B(Index_A);
For example, for the input as follows:
例如,输入如下:
>>
A = {'10.2'; '15.2'; '7.2'};
B = {'m'; 'l'; 'v'};
The result would be:
结果将会是:
>> C
C =
'7.2' 'v'
'10.2' 'm'
'15.2' 'l'
#2
2
The simplest way: use the second output from sort
:
最简单的方法:使用排序的第二个输出:
% your data
a = {
'10.2'
'15.2'
'7.2'
};
b = {
'm'
'l'
's'
};
c = [a b];
% sort the first column, and save the indices
[~,I] = sort(str2double(c(:,1)))
% use the indices to sort the concatenation c:
c(I,:)
#1
4
This would do what you need:
这可以满足你的需要:
>>
C = cell(numel(A),2);
[Sorted_A, Index_A] = sort(cell2mat(A));
C(:,1) = num2cell(Sorted_A);
C(:,2) = B(Index_A);
For example, for the input as follows:
例如,输入如下:
>>
A = {10.2; 15.2; 7.2};
B = {'m'; 'l'; 'v'};
The result would be:
结果将会是:
>> C
C =
[ 7.2000] 'v'
[10.2000] 'm'
[15.2000] 'l'
EDIT
It seems that in your case, the input array A
is not just a cell array of numbers, but a cell array of strings; i.e., for example,
在你的例子中,输入数组A不仅仅是数字的单元数组,而是字符串的单元数组;即。例如,
>>
A = {'10.2'; '15.2'; '7.2'};
In this case, you need to make the following changes:
在这种情况下,你需要做以下改变:
- Use
str2double(A)
instead ofcell2mat(A)
- 使用str2double(A)代替cell2mat(A)
- Use
strtrim(cellstr(num2str(Sorted_A)))
instead ofnum2cell(Sorted_A);
- 使用strtrim(cellstr(num2str(Sorted_A)))代替num2cell(Sorted_A);
Your final code will now be:
您的最终代码现在将是:
>>
C = cell(numel(A),2);
[Sorted_A, Index_A] = sort(str2double(A));
C(:,1) = strtrim(cellstr(num2str(Sorted_A)));
C(:,2) = B(Index_A);
For example, for the input as follows:
例如,输入如下:
>>
A = {'10.2'; '15.2'; '7.2'};
B = {'m'; 'l'; 'v'};
The result would be:
结果将会是:
>> C
C =
'7.2' 'v'
'10.2' 'm'
'15.2' 'l'
#2
2
The simplest way: use the second output from sort
:
最简单的方法:使用排序的第二个输出:
% your data
a = {
'10.2'
'15.2'
'7.2'
};
b = {
'm'
'l'
's'
};
c = [a b];
% sort the first column, and save the indices
[~,I] = sort(str2double(c(:,1)))
% use the indices to sort the concatenation c:
c(I,:)