Append to an array

3 898 vues (au cours des 30 derniers jours)
Subhiksha
Subhiksha le 18 Sep 2013
Commenté : Xuzhaoqiu Zeng le 5 Juin 2021
Hi all,
I have a function called details, where the user enters his name, and the details are stored in the array, say "name". Each time a name is entered it should get appended to the array.
At any point, the user can cancel entering the details and do other computations. Then again the user can start entering the details. In this case, it should get appended to the "name" array that is already existing.
At the beginning of the session, the array "name" should be empty.
  3 commentaires
Omar Ahmed
Omar Ahmed le 25 Mar 2021
Modifié(e) : Omar Ahmed le 25 Mar 2021
Create a 3x3 matrix A in which all the elements are 1, and create a 2x2 matrix B in which all the elements are 5. Then, add elements to the matrix A by appending the matrix B such that A will be:
A = [ 1 1 1 0 0; 1 1 1 0 0; 1 1 1 0 0; 0 0 0 5 5; 0 0 0 5 5]
I need to slove it .
Omar Ahmed
Omar Ahmed le 25 Mar 2021
Haw I can to my code ???

Connectez-vous pour commenter.

Réponse acceptée

Yatin
Yatin le 16 Oct 2013
Modifié(e) : Yatin le 16 Oct 2013
Hi,
A possible solution to your problem could be something like this. Set the array as empty initially(beginning of the session).
nameArray = {};
You can then append the entries in the array as follows:
nameArray = [nameArray, 'Name you want to append'];
This uses the concept of cell array. You might want to refer to the documentation on cell arrays for more information. http://www.mathworks.com/help/matlab/cell-arrays.html
  6 commentaires
bla bla
bla bla le 13 Avr 2020
@Bill Tubbs maybe try nameArray(end+1) = {'name to be appended'}; should be a more efficient way of doing it
Xuzhaoqiu Zeng
Xuzhaoqiu Zeng le 5 Juin 2021
testFactor = 194225602
testStart = 3928
tic
mytest = [testStart];
for i = 1:100000
mytest = [mytest, testStart];
end
toc
tic
mytest = [testStart];
for i = 1:100000
mytest(end+1) = testStart;
end
toc
The result is :
2.024820
0.011121
list = [list, appendedNumber] is ridiculous.

Connectez-vous pour commenter.

Plus de réponses (1)

Vinaya Babu Yerneni
Vinaya Babu Yerneni le 16 Oct 2020
Firstly you need to clarify whether you need to append strings or integer or string and integer
for only strings
name=input('What is your name? ', 's');
age=input('What is your age? ',"s");
hometown=input('What is your hometown? ', 's');
sport=input('What is your favorite sport? ', 's');
Temptex2=append(name,age,hometown,sport)

Catégories

En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by