フィルターのクリア

How to store a variable no. of data points in a matrix?

1 回表示 (過去 30 日間)
roro6992
roro6992 2014 年 1 月 18 日
コメント済み: roro6992 2014 年 1 月 18 日
Hello everyone. The question is..suppose i am using a for loop. At the end of each loop it is giving me a data point only if a certain condition is satisfied otherwise not. I want to store these data points in a k*2 matrix where value of k depends on the condition being satisfied or not.So the no. of data points generated is not previously known. Is is possible to store the data points in a matrix if we dont know the exact dimension of the matrix at the first place?? Please help! Thanx!!

採用された回答

Jan
Jan 2014 年 1 月 18 日
Create the output with a maximum size at first to avoid the extremely time-consuming iterative growing of an array:
data = zeros(N, 2);
index = 0;
for k = 1:N
if FLAG % Apply your condition here
index = index + 1;
data(index, 1:2) = rand(1,2); % Your values to be stored
end
end
data = data(1:index, :); % Crop unused elements
This is much faster, when the size of the output is over a certain limit. Notice that for the iterative growing of a 1000x2 array, you have to obtain sum(1:1000)*2*8 bytes from the operating system and copy the same amount of data: more than 800kB althought the result has 16kB only.
  1 件のコメント
roro6992
roro6992 2014 年 1 月 18 日
Yea it works nice. Thanx alot!!

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

その他の回答 (1 件)

Mischa Kim
Mischa Kim 2014 年 1 月 18 日
Sure. Simply append the data point to the existing matrix of data points:
for ii = 1:N
...
if FLAG % if condition is true, append data point to matrix
data = [data; data_point];
...
end
end
Make sure that data_point is a 1x2 (row) vector.
  1 件のコメント
roro6992
roro6992 2014 年 1 月 18 日
But i want the data matrix to be of the same dimension of the no. of selected data points. I mean if k points are selected the matrix wud be kx2.

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

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by