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 件のコメント

Stephen23
Stephen23 2021 年 4 月 15 日
編集済み: Stephen23 2021 年 4 月 15 日
"There are 24 array in this file. Array names like y1, y2 ... "
The most important thing is to load into an output variable:
S = load(...);
then you can trivially access the fieldnames of the structure S:
Loop over the fieldnames, allocate the data to an array, perform your calculation on the array. Done.
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 日

0 投票

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 for your answer. I think I can use the code you wrote first, but what I really want here is to automatically calculate the average of the first N elements of each array. If I use the first code, I have to write 24 lines of code for the 24 arrays I have. the arrays I have are not in a matrix.
SungJun Cho
SungJun Cho 2021 年 4 月 15 日
Can you provide your .mat file so that I can take a look at it?
Jan
Jan 2021 年 4 月 15 日
" the arrays I have are not in a matrix." - This is the main problem. If thevariables are store in a matrix, the processing is trivial. So this is the solution: Store the data in arrays.
studentmatlaber
studentmatlaber 2021 年 4 月 15 日
I don't know how to store it in a matrix. how can I do it?
SungJun Cho ı can not share the .mat file from here.
SungJun Cho
SungJun Cho 2021 年 4 月 15 日
You can try clicking "Insert" and attach your .mat file in the comment.
SungJun Cho
SungJun Cho 2021 年 4 月 15 日
As Jan mentioned, it is best to store the variables in matrix for convenience. For now, you can do
data_path = '../Downloads/Data_x30_y30_28.mat'; % change this path to where your data is located
data = struct2cell(load(data_path));
data = data(30:53); % select y arrays
Y = cat(1,data{:}); % storing data into a matrix format
mean_mat = mean(Y(:,1:N),2); % given each row is an array
I believe this will solve your problem for now.
studentmatlaber
studentmatlaber 2021 年 4 月 15 日
thank you very very much, but the only thing I don't understand is 30:53. Can you explain here?
SungJun Cho
SungJun Cho 2021 年 4 月 15 日
No problem!
I imported your data as a structure type, then converted it into a cell type using "struct2cell". Once the data was converted, I noticed that the arrays named "y" (e.g., y1, y10, etc.) were located from 30th to 53rd rows of the data, so I extracted only those arrays so that the variable "Y" contains only the y arrays.
studentmatlaber
studentmatlaber 2021 年 4 月 15 日
thank you so much. have a nice day.

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

質問済み:

2021 年 4 月 15 日

コメント済み:

Rik
2021 年 4 月 23 日

Community Treasure Hunt

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

Start Hunting!

Translated by