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.

 채택된 답변

Walter Roberson
Walter Roberson 2023년 1월 20일
편집: Walter Roberson 2023년 1월 20일

0 개 추천

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, ','))
numvals = 34×1
0 50 37 43 45 71 86 84 79 84
mean(numvals)
ans = 69.8824

댓글 수: 1

Walter Roberson
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
the cyclist 2023년 1월 20일

0 개 추천

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)
ans = 30×1
0.5556 0.4111 0.4778 0.5000 0.7889 0.9556 0.9333 0.8778 0.9333 0.9667

댓글 수: 2

Brave A
Brave A 2023년 1월 20일
I need how many values in each row then the average not the number of rows. I know they are 30 rows.
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)
rowCount = 30×1
50 37 43 45 71 86 84 79 84 87
meanRowCount = mean(rowCount)
meanRowCount = 79.2000

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

태그

질문:

2023년 1월 20일

댓글:

2023년 1월 20일

Community Treasure Hunt

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

Start Hunting!

Translated by