필터 지우기
필터 지우기

Counting the Size of X Indices of a Matrix

조회 수: 3 (최근 30일)
Patrick Faulkner
Patrick Faulkner 2020년 11월 6일
답변: madhan ravi 2020년 11월 6일
I am trying to find out how to input a random matrix of random dimensions and a random number of entries in each dimension, and output "dimension a = 5 entries, dimension b = 4 entries" etc without previously specifying how many there will be.
So for example, if I have matrix A which is 4x5x2 (for whatever reason), I want a series of operations that will output: Dimension A = 4, Dimension B = 5, Dimension C = 2". I have been trying to do this with "F = Size(A)", except I have to input "[a, b, c] = Size(A)" to get the output I want. And I can't find a way to input a matrix (so that it identifies the size of each dimension) without already saying how many dimensions there are.
Here's the function I have right now:
function Plethos = Plethos(X)
% This function will tell you what the plethos of a Holor index is.
Number = Valence(X);
Start = 97;
End = 97 + Number - 1;
if Start == End;
Index = char(Start)
else
Index = char(Start:End)
end
Final = End - Start + 1
%-------------------------------------------%
Index2 = str2double(Index)
Index2 = sym('Index', [1, Final])
Index2 = size(X)
end
If you're wondering, Valence(X) is a function I have which already counts how many dimensions (or indices) a matrix has, from the scalar (Valence = 0), to a vector (Valence = 1), to a matrix (Valence = 2), to a tensor/4x5x2 matrix (Valence = 3), etc.
Everything goes perfectly, so that "Index" becomes "abcde...." until I have a letter for each dimension of the input. Everything is perfect until the comment line. I tried using sym(A) to make a matrix out of the letters with them as variables, but all of the str2___ functions have a null output, and even if they didn't, Index2 = size(X) (which is meant to perform [a b c] = size(X) so that the output gives a = 4, b = 5, c = 2) doesn't work that way. So I would like help on how to make a value which contains a matrix of variables, which will then be used to measure each dimension of the input just like [a b c] = Size (X) would normally output.

채택된 답변

madhan ravi
madhan ravi 2020년 11월 6일
Naming variables is a bad idea
Dimensions = num2cell(size(rand(2,3,4))) % simply use a cell array
Dimensions = 1x3 cell array
{[2]} {[3]} {[4]}
celldisp(Dimensions)
Dimensions{1} = 2 Dimensions{2} = 3 Dimensions{3} = 4

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by