简体   繁体   English

列表MATLAB中的项目数

[英]Number of Items in List MATLAB

argument = [new_letter_A, new_letter_B, new_letter_C, new_letter_D, new_letter_E];

In Python, I can use len(argument) to find the number of items in the array above. 在Python中,我可以使用len(argument)在上面的数组中查找项目数。 Is there an easy way to accomplish the same thing in MATLAB? 有没有一种简单的方法可以在MATLAB中完成相同的任务? (I want this to return '5'.)Thank you. (我希望它返回“ 5”。)谢谢。

You should either store your vectors as rows and get the size of the first dimension 您应该将向量存储为行并获取第一维的大小

argument = [new_letter_A; new_letter_B; new_letter_C; new_letter_D; new_letter_E];
size(argument, 1)

Or you can store each vector as a cell in a cell array 或者您可以将每个向量存储为单元格数组中的单元格

argument = {new_letter_A, new_letter_B, new_letter_C, new_letter_D, new_letter_E};
length(argument)

One of the advantages of the second approach is that you can use cellfun to apply a function to each letter (for example, if you had a function which you were using to compress each letter...) 第二种方法的优点之一是您可以使用cellfun将函数应用于每个字母(例如,如果您有一个用于压缩每个字母的函数...)

使用length功能:

length(argument)

There are many way to do this such as numel , length and size . 有很多方法可以做到这一点,例如numellengthsize MATLAB works with 2d arrays/matrices. MATLAB适用于2d数组/矩阵。

If your matrix is nxm : 如果您的矩阵是nxm

  • numel would be n*m numel是n * m
  • length would be max(n, m) length为max(n,m)
  • size would be n. size为n。 You can use size(argument, 2) to get m. 您可以使用size(argument,2)来获得m。

In the case of 1-d array, they are all the same. 对于一维数组,它们都是相同的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM