Why is my for loop code not incrementing correctly?

2 ビュー (過去 30 日間)
Raushan
Raushan 2022 年 9 月 15 日
コメント済み: Walter Roberson 2022 年 9 月 15 日
Hi!
In the code below, the data1 matrix has 100000 data in each of the two columns. I want to read the second column and divide whole column 2 with a gap of 256. That means, I want data from 1 to 256 in one matrix and then 257 to 512 in another and so on. But, the for loop that I have used is giving me only one output of the data , ie one matrix. I want to have all these data separted with a record size of 256 in differnet tables or matrices. What am i missing here?
start= 1;
last= 256;
for n= 1:1:389
% fprintf("%d \n", n)
n = data1(start:last, 2);
start= last+1;
last= start+ 255;
end
Any help would be very much appreciated.

回答 (1 件)

Walter Roberson
Walter Roberson 2022 年 9 月 15 日
N = 256;
last = floor(size(data1,2)/N) * 256;
buffered = reshape(data1(1:last, 2), N, []);
The entries will now be down columns.
| want to have all these data separted with a record size of 256 in differnet tables or matrices.
That would require creating variables dynamically, which we highly recommend not be done.
  1 件のコメント
Walter Roberson
Walter Roberson 2022 年 9 月 15 日
Please read http://www.mathworks.com/matlabcentral/answers/304528-tutorial-why-variables-should-not-be-named-dynamically-eval for information about why we strongly recommend against creating variable names dynamically.

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

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by