How to add elements to a list

1.183 visualizaciones (últimos 30 días)
Winston DeGraw
Winston DeGraw el 15 de Jun. de 2017
Movida: Voss el 25 de Jun. de 2024
I've tried several different options for adding elements to a list, examples:
ls=[]
ls=[ls,element];
Which served only to concatenate the elements that I added, with the same issue happening here:
ls=append(ls,element);
Any other options?
  1 comentario
Stephen23
Stephen23 el 16 de Jun. de 2017
Editada: Stephen23 el 16 de Jun. de 2017
  1. MATLAB does not have any "list" data type. The common MATLAB data types are arrays: numeric, cell, struct, etc.
  2. [] is an array concatenation operator, not a list operator, as the documentation clearly describes.
  3. Adding an element to an array can be achieved using indexing or concatenation. What behavior do you expect that you are not seeing?
The best place to learn about very basic MATLAB concepts, such as what data types there are and how to access them, is to do the introductory tutorials (which are highly recommended for all beginners):

Iniciar sesión para comentar.

Respuesta aceptada

Walter Roberson
Walter Roberson el 16 de Jun. de 2017
ls(end+1) = element;
"Which served only to concatenate the elements that I added"
What behavior are you hoping for that you are not seeing?
  2 comentarios
Stephen23
Stephen23 el 16 de Jun. de 2017
Surely this has the same effect as the code shown in the question?
Walter Roberson
Walter Roberson el 16 de Jun. de 2017
They asked "Any other options?"

Iniciar sesión para comentar.

Más respuestas (1)

Korosh Agha Mohammad Ghasemi
Korosh Agha Mohammad Ghasemi el 25 de Jun. de 2024
Movida: Voss el 25 de Jun. de 2024
To efficiently add elements to a list (or array) in MATLAB without causing concatenation issues, you should use cell arrays or preallocate memory for your array if you know the number of elements in advance. Here are some different methods to add elements to a list in MATLAB:
Using Cell Arrays
If you don't know the number of elements in advance or if the elements are of different types, using a cell array is a good approach:
ls = {}; % Initialize an empty cell array
element = 'newElement'; % Example element
% Adding elements to the cell array
ls{end+1} = element;
% Display the cell array
disp(ls);
Preallocating Memory
If you know the number of elements you will be adding, preallocate memory to avoid resizing the array each time you add an element:
n = 10; % Assume you want to add 10 elements
ls = zeros(1, n); % Preallocate an array of zeros with size 1x10
for i = 1:n
ls(i) = i; % Assign values to the array
end
% Display the array
disp(ls);
Using Dynamic Arrays
If you need to dynamically grow your array without preallocation, you can use the end+1 indexing for numeric arrays:
ls = []; % Initialize an empty array
element = 5; % Example element
% Adding elements to the array
ls(end+1) = element;
% Display the array
disp(ls);
Using append Function with Strings
If you're working with strings and need to append them, use the strcat or append function:
str = ""; % Initialize an empty string
element = "newElement"; % Example element
% Append the element to the string
str = append(str, element);
% Display the string
disp(str);
Example Combining Different Elements
Here’s a combined example that dynamically adds elements to a cell array:
ls = {}; % Initialize an empty cell array
% Elements to be added
elements = {1, 'text', [1, 2, 3], {4, 5}, 6.7};
% Adding elements to the cell array
for i = 1:length(elements)
ls{end+1} = elements{i};
end
% Display the cell array
disp(ls);
Each of these methods allows you to add elements to a list (or array) in MATLAB without running into concatenation issues. Choose the method that best fits your specific needs.

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by