How to get the matrix with maximum(or minimum) number of elements

3 ビュー (過去 30 日間)
Chamira Wickramasinghe
Chamira Wickramasinghe 2016 年 2 月 27 日
コメント済み: Stephen23 2016 年 2 月 27 日
Hi,
Suppose there are 3 matrixes A,B & C with sizes of [1 5],[1 8] and [1 9] respectively.
Is it possible to find the matrix with the maximum number of elements. Here it is C
This is the actual code I'm working on. Here I want to find that biggest matrix(y1 or y2). I know it can be done by typing length(y1) and length(y2) and choose the matrix with biggest one answer. But what I want to do is that I want to get it as an answer
load('chirp.mat');
y1 = transpose(y);
load('gong.mat');
y2 = transpose(y);
Thank you.

回答 (2 件)

Stephen23
Stephen23 2016 年 2 月 27 日
編集済み: Stephen23 2016 年 2 月 27 日
It is much easier to perform these kind of operations when all of the data are in one variable. So that is the first thing to do: lets put them all into one cell array (this is perhaps how they should have been saved anyway):
>> A = randi(9,1,5);
>> B = randi(9,1,8);
>> C = randi(9,1,9);
>> D = {A,B,C}; % put into a cell array
Now finding the array with the most elements is trivial:
>> N = cellfun(@numel,D)
N =
5 8 9
>> [~,X] = max(N); % its index
>> D{X} % extract that array
ans =
5 7 7 6 1 1 3 5 6
  2 件のコメント
Chamira Wickramasinghe
Chamira Wickramasinghe 2016 年 2 月 27 日
thank u :)
Stephen23
Stephen23 2016 年 2 月 27 日
My pleasure. You can also accept the answer the best resolves your question.

サインインしてコメントする。


John BG
John BG 2016 年 2 月 27 日
try
max(max(size(y1),size(y2)))
If you find this answer of any help solving your question, please click on the thumbs-up link, thanks in advance
John
  1 件のコメント
Chamira Wickramasinghe
Chamira Wickramasinghe 2016 年 2 月 27 日
Sir,
Thank u very much for answer. But I'm afraid to say that it doesn't solve my problem. It returns the maximum value. What I want is to return what the biggest matrix is. In my code it should return 'y2'

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by