单元格数组中的组数据

发布于 2025-01-29 10:36:50 字数 344 浏览 4 评论 0原文

我有以下数据:

Names={A1 A2 B1 B2 C1 C2 C3}
Doserate=(2.2 3.4 6.4 3.4 2.3 4.5 7.5)
Time=(5 7.8 9 3.5 10.2 5.6 7.8)

剂量time的顺序与name相对应。我想将小组从相同的字母开始,以便可以使用doserate时间对应于该组的计算。 名称可以变化到更多字母(AZ)或更多数字(例如(A1-A30))。

我该如何分组这些条目?

I have the following data:

Names={A1 A2 B1 B2 C1 C2 C3}
Doserate=(2.2 3.4 6.4 3.4 2.3 4.5 7.5)
Time=(5 7.8 9 3.5 10.2 5.6 7.8)

The order of Doserate and Time is such they correspond to Names. I would like to make groups starting with the same letter so that I can perform calculations using Doserate and Time corresponding to that group. Names can vary to even more letters (A-Z) or more numbers like (A1-A30).

How can I group these entries?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ま柒月 2025-02-05 10:36:50
Names={'A1' 'A2' 'B1' 'B2' 'C1' 'C2' 'C3'};
first_letter = blanks(numel(Names));
for ii = 1:numel(Names)
    first_letter(ii) = Names{ii}(1);  % Grab the first letter
end
[a, b, c] = unique(first_letter)

a =

ABC


b =

     2     4     7


c =

     1     1     2     2     3     3     3

您可以使用循环提取每个单元格中的第一个字符(您可能还可以使用cellfun()),然后调用unique()提取独特的字符。在我的示例中,它的第三个输出名为c将是您的组。因此,剂量(C(C == 2))将返回所有剂量>剂量>条目,其中名称b开始。

Names={'A1' 'A2' 'B1' 'B2' 'C1' 'C2' 'C3'};
first_letter = blanks(numel(Names));
for ii = 1:numel(Names)
    first_letter(ii) = Names{ii}(1);  % Grab the first letter
end
[a, b, c] = unique(first_letter)

a =

ABC


b =

     2     4     7


c =

     1     1     2     2     3     3     3

You can use a loop to extract the first character in each cell entry (you can probably use cellfun() as well) and then a call to unique() to extract the unique characters. Its third output, named c in my example, will be your groups. Thus Doserate(c(c==2)) will return all Doserate entries where Names start with B.

鸢与 2025-02-05 10:36:50

要从名称的开头提取一个或多个字母,您可以使用正则是:

Names = {'A1' 'A2' 'B1' 'B2' 'C1' 'C2' 'C3'};
N = regexp( Names, '^[a-zA-Z]+', 'match', 'once' )
% >> N = {'A', 'A', 'B', 'B', 'C', 'C', 'C'};

然后,您可以使用findgroups对它们进行分组

[gidx,gnames] = findgroups(N)

gidx =
     1     1     2     2     3     3     3
gnames =
    {'A', 'B', 'C'}

,即gidx == 2与之匹配组'B'gnames中的第二个元素)。

To extract one or more letters from the start of your names, you can use regex:

Names = {'A1' 'A2' 'B1' 'B2' 'C1' 'C2' 'C3'};
N = regexp( Names, '^[a-zA-Z]+', 'match', 'once' )
% >> N = {'A', 'A', 'B', 'B', 'C', 'C', 'C'};

Then you can use findgroups to group them

[gidx,gnames] = findgroups(N)

gidx =
     1     1     2     2     3     3     3
gnames =
    {'A', 'B', 'C'}

i.e. now anything where gidx == 2 matches the group 'B' (2nd element in gnames).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文