How can I manage dynamic array size ?

2 ビュー (過去 30 日間)
sufian ahmed
sufian ahmed 2017 年 7 月 23 日
コメント済み: Image Analyst 2021 年 5 月 10 日
I face a problem. I call a function but function returns different number of values for ex: sometimes return 9 or 8 or 7 etc. but after returning i kept the value in a array. for ex: f_array(i:)=d(A)...here i=1,2,3 etc. But i cant define a fixed size for f_array. because when i fixed its size & then if the return arrays size not matched ,then it gives error. i cant define its size like: size=[]. when i define in this way , for first returning array it gives no error, but for 2nd it gives error because first_array_size != second_array_size. How can i deal with this problem ?

回答 (4 件)

Matt J
Matt J 2017 年 7 月 23 日
編集済み: Matt J 2017 年 7 月 23 日
Store the output in a cell array
f_cell{i}=d(A)
  3 件のコメント
Matt J
Matt J 2017 年 7 月 25 日
It's not really sensible for the data that you describe, unless perhaps you have many columns of nearly the same size.
Image Analyst
Image Analyst 2017 年 7 月 25 日
f_cell is one array.

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


Image Analyst
Image Analyst 2017 年 7 月 23 日
This is what cell arrays are for: storing variables of variable sizes and data types. Read the FAQ for a good tutorial: http://matlab.wikia.com/wiki/FAQ#What_is_a_cell_array.3F

Walter Roberson
Walter Roberson 2017 年 7 月 23 日
temp = d(A);
farray(i, 1:length(temp)) = temp;
The above will make farray wider if necessary, filling in 0 in columns. If you need to have NaN or something else filled to indicate "not used" then the code becomes slightly longer.
  2 件のコメント
sufian ahmed
sufian ahmed 2017 年 7 月 23 日
編集済み: sufian ahmed 2017 年 7 月 23 日
i want to use NAN... how can i do ? Basically i want to do this like take column size as the higher returning number of values. if have lower number of values then this will fill by 'NAN'.. i try u r code but it gives error
Walter Roberson
Walter Roberson 2017 年 7 月 23 日
da = d(A);
Lda = length(da);
widthf = size(farray,2);
if Lda <= widthf
farray(i, 1: Lda) = da;
farray(i, Lda+1 : widthf) = nan;
else
farray(: widthf+1 : Lda) = nan;
farray(i, 1 : Lda) = da;
end
The above code does not assume that farray has been allocated with the maximum possible width. In the special case that you know that it has been allocated as the widest possible and that the maximum number of rows have been assigned, then initialize the whole thing to nan, like
widest_possible = 9;
maxrows = 173;
farray = nan(maxrows, widest_possible);
and after that storing each iteration becomes just
da = d(A);
farray(i, 1 : length(da)) = da;
If you do not know the maximum number of rows (an inefficient case!) but you did initialize to the widest, like
widest_possible = 9;
farray = zeros(0, widest_possible);
and you are (inefficiently!) growing by rows as you go, then you can use
farrary(i, :) = nan;
da = d(A);
farray(i, 1 : length(da)) = da;

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


amal laabidi
amal laabidi 2021 年 5 月 8 日
編集済み: Image Analyst 2021 年 5 月 8 日
How do I create a dynamic table to store values measured using a mpu6050 sensor in MATLAB?
I transfered the measured data using a mpu6050 sensor to MATLAB to display it. Now, how do I create a table to sotcker this data?
  6 件のコメント
amal laabidi
amal laabidi 2021 年 5 月 10 日
do you know how to declare a dynamic array ???
Image Analyst
Image Analyst 2021 年 5 月 10 日
@amal laabidi What exactly does that (dynamic array) mean to you? Any array can be dynamic in that it's possible for you to change the values during run-time.

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

カテゴリ

Help Center および File ExchangeImage Processing Toolbox についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by