Filter löschen
Filter löschen

How to make empty array of specific size?

470 Ansichten (letzte 30 Tage)
Zeynab Mousavikhamene
Zeynab Mousavikhamene am 14 Okt. 2019
Bearbeitet: HAG am 5 Okt. 2023
a=ClassName.empty(2,2)
but I got this error:
Undefined variable "ClassName" or class "ClassName.empty".
  2 Kommentare
Walter Roberson
Walter Roberson am 14 Okt. 2019
You would have to substitute the name of the class for ClassName
Zeynab Mousavikhamene
Zeynab Mousavikhamene am 14 Okt. 2019
Bearbeitet: Zeynab Mousavikhamene am 14 Okt. 2019
@Walter Roberson I changed to a.empty(2,2). But now a=0 and isempty(a)=0. Why? It should be ''empty'', not zero

Melden Sie sich an, um zu kommentieren.

Antworten (4)

Walter Roberson
Walter Roberson am 14 Okt. 2019
Bearbeitet: Walter Roberson am 9 Apr. 2023

Steven Lord
Steven Lord am 14 Okt. 2019
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 Kommentare
Zeynab Mousavikhamene
Zeynab Mousavikhamene am 14 Okt. 2019
@ 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 am 14 Okt. 2019
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);

Melden Sie sich an, um zu kommentieren.


Linnéa Haugen
Linnéa Haugen am 9 Apr. 2023
Bearbeitet: Linnéa Haugen am 9 Apr. 2023
I found another solution that starts out empty. i also wanted to create an empty array, with 2 columns but of unknown length.
arr = [[], []];
if ..
else
arr(end+1,1) = a;
end
if..
arr(end,2) = b;
end
a and b are changing in a for loop and i only need to save some values. i'm finding peaks and if i find one i save to location (a) and change the hight by the amount (b).
I only have an interest in the values i change, and i don't know how many i'll change in advance.

HAG
HAG am 5 Okt. 2023
Bearbeitet: HAG am 5 Okt. 2023
To create an empty array of any length, use zeros function
zeros(4,0)
ans = 4×0 empty double matrix

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by