MATLAB Answers

how do I put arrays into a for loop

1 ビュー (過去 30 日間)
studentmatlaber
studentmatlaber 2021 年 4 月 15 日
コメント済み: Rik 2021 年 4 月 23 日
I have a .mat file. There are 24 array in this file. I want to calculate the mean of the first N elements of an array.
But what I want is for the 24 directories in the .mat file to automatically enter this into a for loop. I am waiting for your help in this matter.
  2 件のコメント
Rik
Rik 2021 年 4 月 23 日
Backup of this question:
how do I put arrays into a for loop
I have a .mat file. There are 24 array in this file. I want to calculate the mean of the first N elements of an array.
But what I want is for the 24 directories in the .mat file to automatically enter this into a for loop. I am waiting for your help in this matter.

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

採用された回答

SungJun Cho
SungJun Cho 2021 年 4 月 15 日
編集済み: SungJun Cho 2021 年 4 月 15 日
To calculate the mean of the first N elements of an array, you can use
mean_y1 = mean(y1(1:N));
However, you may import a .mat file which I suppose will give you a matrix in which each row or column is one array. In that case, just perform
mean_mat = mean(Y(:,1:N),2); % if each row is an array
mean_mat = mean(Y(1:N,:),1); % if each column is an array
and you will get a matrix containing the mean of first N elements of each array.
This should be a more efficient way to compute the mean than to use a for loop, but if using a for-loop is necessary, feel free to let me know.
  9 件のコメント
studentmatlaber
studentmatlaber 2021 年 4 月 15 日
thank you so much. have a nice day.

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

その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by