MATLAB Answers

How to make empty array of specific size?

2,038 ビュー (過去 30 日間)
Zeynab Mousavikhamene
Zeynab Mousavikhamene 2019 年 10 月 14 日
回答済み: Walter Roberson 2019 年 10 月 14 日
a=ClassName.empty(2,2)
but I got this error:
Undefined variable "ClassName" or class "ClassName.empty".

  2 件のコメント

Walter Roberson
Walter Roberson 2019 年 10 月 14 日
You would have to substitute the name of the class for ClassName
Zeynab Mousavikhamene
Zeynab Mousavikhamene 2019 年 10 月 14 日
@Walter Roberson I changed to a.empty(2,2). But now a=0 and isempty(a)=0. Why? It should be ''empty'', not zero

サインイン to comment.

回答 (2 件)

Steven Lord
Steven Lord 2019 年 10 月 14 日
Replace ClassName with the name of the class for which you want to create the empty array.
But in order to use the empty method, the size of the array you want to create must be 0 in at least one of its dimensions. You can't, for example, have a 2-by-2 empty array. If you want to make a double 2-by-2 array, use zeros, ones, rand, eye, etc.
x = zeros(2); % 2-by-2 double array filled with 0's
y = ones(2, 'single'); % 2-by-2 single array filled with 1's
z = int8.empty(2, 2, 0) % 2-by-2-by-0 int8 array

  2 件のコメント

Zeynab Mousavikhamene
Zeynab Mousavikhamene 2019 年 10 月 14 日
@ Steven Lord Thanks but as I said in the question, for some reason I dont want to create with numbers like zero or one and it seems matlab does not have that capability.
Steven Lord
Steven Lord 2019 年 10 月 14 日
If the array has at least one element, those elements must have a value. Let's say you created your "empty" 2-by-2 matrix a. What specifically do you want the value of q to be after running the following line of code?
q = a(1, 1);

サインイン to comment.


Walter Roberson
Walter Roberson 2019 年 10 月 14 日
https://www.mathworks.com/matlabcentral/fileexchange/31362-uninit-create-an-uninitialized-variable-like-zeros-but-faster

  0 件のコメント

サインイン to comment.

サインイン してこの質問に回答します。

タグ


Translated by