Finding the matrix with the most elements?

3 ビュー (過去 30 日間)
Andrew
Andrew 2015 年 1 月 7 日
コメント済み: Image Analyst 2015 年 1 月 7 日
Hello all,
I'm kinda new to matlab so I'm still trying find my way through the way things work. That said I have a question that I need some help on:
I have 3 matrix arrays of varying sizes, called say V1, V2, and V3. How would I go about programming the code so that matlab finds the matrix with the most elements.
I want to save the number of elements in the largest matrix as it's own variable, "LarMat", and I know how to do that already, it's just that finding the largest of the three is giving me some trouble.
  1 件のコメント
Azzi Abdelmalek
Azzi Abdelmalek 2015 年 1 月 7 日
V1=[1 2;3 4]
V2=[0 1 2;3 4 1]
V3=[5 6 0 1;2 3 0 1;1 1 0 1]
What would be the result?

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

採用された回答

Star Strider
Star Strider 2015 年 1 月 7 日
This is one way:
V1 = rand(2,2);
V2 = rand(3,3);
V3 = rand(4,4);
[els,idx] = max([numel(V1) numel(V2) numel(V3)]);
The ‘els’ variable will tell you the maximum number of elements, and the ‘idx’ variable will tell you which one. (It is ‘V3’ — the third matrix whose elements are determined — here.)
  3 件のコメント
Star Strider
Star Strider 2015 年 1 月 7 日
My pleasure!
Thank you for the compliment.
Image Analyst
Image Analyst 2015 年 1 月 7 日
Andrew, what was the method you were using to find "LarMat"?

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

その他の回答 (2 件)

Image Analyst
Image Analyst 2015 年 1 月 7 日
Try this:
% Check the number of elements against LarMat,
% which you say that you've computed already (or at least know how to).
if LarMat == numel(V1)
fprintf('V1 is the largest.\n');
elseif LarMat == numel(V2)
fprintf('V2 is the largest.\n');
else
fprintf('V3 is the largest.\n');
end

Chad Greene
Chad Greene 2015 年 1 月 7 日
V1 = rand(1,5);
V2 = rand(4,3);
V3 = magic(2);
% Number of elements in each matrix:
numels = [numel(V1) numel(V2) numel(V3)];
% Max number of elements per matrix:
LarMat = max(numels)
% Matrix with most elements:
BiggestMatrix = find(numels==LarMat)

カテゴリ

Help Center および File ExchangeOperators and Elementary Operations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by