Making Dynamic array

439 ビュー (過去 30 日間)
Khawaja Asim
Khawaja Asim 2011 年 8 月 13 日
I want to creat an array that is dynamic in size. I mean I dont know what is going to be the size of array. I just want to insert every upcoming number on the head of array. Tell me the syntax of this and show me the code to find max num in this array at the end. Thanks

採用された回答

Oleg Komarov
Oleg Komarov 2011 年 8 月 13 日
編集済み: John Kelly 2015 年 2 月 26 日
What do you need it for?
There's no such thing as a dynamic array but you can grow an array with concatenation which is usually not recommended (or was not recommended).
If you have:
A = rand(10,1);
You can grow it as:
A = [A; rand(5,1)];
Such practice very often can be avoided.
Read the first 4 chapters of the getting started guide to see more examples of basi array operations.
  3 件のコメント
Oleg Komarov
Oleg Komarov 2011 年 8 月 13 日
In the command vindow type:
doc max
Oleg Komarov
Oleg Komarov 2011 年 8 月 13 日
If you know k and it's last value then you don't need to grow it and as I said this practice is (was) not recommended:
for k = 1:10
A(k) = k;
end
This copy-writes the entire array at each iteration and it lead to performance degradation. With the release 2011a things changed.

サインインしてコメントする。

その他の回答 (4 件)

Hooman Sedghamiz
Hooman Sedghamiz 2017 年 2 月 10 日
編集済み: Hooman Sedghamiz 2017 年 2 月 10 日
As others correctly noted, it is not a good practice to use a not pre-allocated array as it highly reduces your running speed.
Solution 1: In fact it is possible to have dynamic structures in Matlab environment too. However, it is not a native Matlab structure. Recently, I had to write a graph traversal script in Matlab that required a dynamic stack. To do so, you can simply use a Stack from java libraries for example. It is quite powerful and you can handle almost all different types of data! You can simply borrow this from java within matlab by;
% imports stack utility from java in matlab
import java.util.*;
% then use following commands to deal with stack
A = Stack();
A.push(i); % inserts i on top of stack A
A.pop(); %pops out an element
Solution 2: When I deal with arrays that I do not have any idea how big they have to be, I normally initialize them with an overestimated large size, then assign a counter to them and remove the nonused part in the end of the script. For example:
% Let A be the array that i am going to use, Initialize to large number
A = zeros(1,1000000);
% assign a counter that tracks the actual size of your array
counter = 1;
% an arbitrary while loop
rn = 0;
while rn ~= 8
A(counter) = rn;
rn = randi(10,1,1);
counter = counter + 1;
end
A = A(1:counter-1); % removes the rest of non used array
Hope that was what you were looking for! Cheers
  2 件のコメント
Zeljko Tomicevic
Zeljko Tomicevic 2018 年 1 月 2 日
編集済み: Zeljko Tomicevic 2018 年 1 月 3 日
Hooman Sedghamiz is it the same with two dimensional array? For example:
% Let A be the array that i am going to use, Initialize to large number
A = zeros(1,1000000);
% assign a counter that tracks the actual size of your array
counter = 1;
% an arbitrary while loop
rn = 0;
while rn ~= 8
A(:, counter) = zeroes(1,11);
rn = rn + 1;
counter = counter + 1;
end
end
That example works in Matlab but not in Simulink User defined function block Matlab fcn. Simulink returns: Subscripted assignment dimension mismatch: [1] ~= [11]
Best
Guillaume
Guillaume 2018 年 1 月 3 日
"That example works in Matlab" Not with my version of matlab!
Note that since you defined A with one row A(:, counter) is the same as A(counter), it is a scalar. You cannot assign a 1x11 array to a scalar.
As for your question, it would be very unusual to have a 2D dimensional array whose both dimension are unknown ahead of time, so just make the unknown dimension larger and declare the other one the right size to start with:
A = zeros(100000, 11);
counter = 1;
while rand < .95 && counter <= size(A, 1)
A(counter, :) = randi(100, 1, 11);
counter = counter + 1;
end
A(counter:end, :) = [];

サインインしてコメントする。


Khawaja Asim
Khawaja Asim 2011 年 8 月 13 日
Array=[] this leads us to a dynamic array... just run a loop and assign it a value in this way Array(k)=___;
  1 件のコメント
saumya srivastava
saumya srivastava 2019 年 3 月 28 日
thank you

サインインしてコメントする。


Mohit Kumar
Mohit Kumar 2019 年 8 月 21 日
%dynamic matrix
%no need to ask the dimensions from user
%just enter values get the matrix
function matrix = dynamic_matrix()
disp("Enter matrix's element row wise--");
disp('Press enter two times to exit from matrix--');
matrix=[];
while true
str=input('','s');
if isempty(str)
break;
end
[row,~,errmsg]=sscanf(str,'%f'); % getting row in column vector
if ~isempty(errmsg)
error(errmsg);
end
matrix=[matrix row]; % column catenation
end
matrix=matrix'; % transpose for desire result
disp(matrix);
end
  1 件のコメント
Chaudhary P Patel
Chaudhary P Patel 2020 年 2 月 2 日
actually i am going to use dynamics data for analysis of force but i do not know the size of data how can i tackle it. i write a loop for it but when it is a single value only then it is workin but i take whole size like j=1:1:1401 it is showing errors. please guide me

サインインしてコメントする。


William Bless Steven LAWSON
William Bless Steven LAWSON 2022 年 1 月 27 日
<html>
<head>
<script type="text/javascript">
var i = 0;
function addKid()
{
if (i < Infinity)
{
var newRow = document.createElement('tr');
newRow.innerHTML = '<td> <input type="text" name="Designation_'+i+'" ><td> <input type="text" name="Serie_'+i+'" ></td><td><input type="text" name="Quantite_'+i+'" ></td><td><input type="button" id="add_kid()" onClick="addKid()" value="+" /><input type="button" value="-" onclick="removeKid(this.parentNode)"></td>';
document.getElementById('kids').appendChild(newRow);
i++;
}
}
function removeKid(element)
{
document.getElementById('kids').removeChild(element.parentNode);
i--;
}
</script>
</head>
<body>
<table border="1" id="kids">
<tr>
<th>Noms</th>
<th>Prénoms</th>
<th>Date de Naissance</th>
</tr>
<tbody >
<tr >
<td >
<input type="text" name="Designation">
</td>
<td>
<input type="text" name="Serie">
</td>
<td>
<input type="text" name="Quantite">
</td>
<td>
<input type="button" id="add_kid()" onClick="addKid()" value="+" />
</td>
</tr>
</tbody>
</table>
</body>
</html>
  1 件のコメント
William Bless Steven LAWSON
William Bless Steven LAWSON 2022 年 1 月 27 日
You can also use java in an HTML code and put that in an HTML field particular in appdesigner

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by