take the length of each row
2 ビュー (過去 30 日間)
古いコメントを表示
Hi
I have this file whih has values and 30 rows. I need to take the average of the length of each row for the attached file. How to do that? I tried but it gives the average among value and I need the length.
0 件のコメント
採用された回答
Walter Roberson
2023 年 1 月 20 日
編集済み: Walter Roberson
2023 年 1 月 20 日
This code assumes that each line ends with a comma and that the number of "values" is equal to the number of commas. It assumes that there are no missing values on a row, and that no row ends in a numeric value.
S = readlines('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1269270/val.txt');
numvals = cellfun(@length, regexp(S, ','))
mean(numvals)
1 件のコメント
Walter Roberson
2023 年 1 月 20 日
Note: the difference in average between my results and The Cyclist, is because your file starts with a blank line, which is thus a line that contains zero values, and the zero is getting counted in the average.
その他の回答 (1 件)
the cyclist
2023 年 1 月 20 日
There are probably a few ways to do this. Here is one, which relies on reading the file into a numeric array, which will pad the rows with NaN values.
val = readmatrix("https://www.mathworks.com/matlabcentral/answers/uploaded_files/1269270/val.txt");
mean(~isnan(val),2)
2 件のコメント
the cyclist
2023 年 1 月 20 日
Sorry, I misread. I think this slight change to the above code does what you want.
val = readmatrix("https://www.mathworks.com/matlabcentral/answers/uploaded_files/1269270/val.txt");
rowCount = sum(~isnan(val),2)
meanRowCount = mean(rowCount)
参考
カテゴリ
Help Center および File Exchange で Environment and Settings についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!