For Loop Table Matlab
1 view (last 30 days)
Show older comments
Pouyan Sadeghian
on 24 May 2021
Answered: Pouyan Sadeghian
on 6 Jun 2021
Hey everyone,
I am working on a project and don't know how to go further.
My issue:
I have a table with measurements. I add a part of it here:
Number Label MaxPosition MinValue
______ __________________________ ___________ ________
1 {'Image0100 16-28-31.bmp'} 94 0
2 {'Image0100 16-28-31.bmp'} 123 0
3 {'Image0101 16-28-46.bmp'} 95 0
4 {'Image0101 16-28-46.bmp'} 124 0
5 {'Image0102 16-29-01.bmp'} 95 0
6 {'Image0102 16-29-01.bmp'} 125 0
7 {'Image0103 16-29-16.bmp'} 96 0
8 {'Image0103 16-29-16.bmp'} 126 0
9 {'Image0104 16-29-31.bmp'} 98 0
10 {'Image0104 16-29-31.bmp'} 127 0
11 {'Image0105 16-29-46.bmp'} 98 0
12 {'Image0105 16-29-46.bmp'} 128 0
13 {'Image0106 16-30-01.bmp'} 98 0
14 {'Image0106 16-30-01.bmp'} 128 0
15 {'Image0107 16-30-16.bmp'} 99 0
16 {'Image0107 16-30-16.bmp'} 129 0
17 {'Image0108 16-30-31.bmp'} 100 0
18 {'Image0108 16-30-31.bmp'} 130 0
In the coloumn "Label" you see the image with his index and in the column "MaxPosition" you see values.
Now I want to determine the mean value of MaxPosition for every image.
For example for the first image Image0100 the mean of the MaxPosition values -> 94+123/2 and so on.
Because I have a lot of measurements (>1000) it is impossible to calculate the mean of every image manually. I want to write a script.
I thought about to use a for loop like:
for every row in the table calculate the mean of the images with the same index or calculate the mean of the two following rows. The first case would be great but it is difficult to implement. So tried for the second case:
for n = 1:2:height(table) % I want to write from row 1 till the last row in two steps
mean(MaxPosition(n), MaxPosition(n+1))
end
As you see I am a beginner and I dont know how to realize my issue.
If someone have a hint or could help me to solve my problem I would be very thankfull.
Best
P.
0 Comments
Accepted Answer
David Hill
on 24 May 2021
data=readtable('Results.csv');
m=mean(reshape(data.MaxPosition,2,[]));%assuming two consecutive labels for all pictures
More Answers (3)
David Hill
on 24 May 2021
m=mean(reshape(T.MaxPosition,2,[]));%assuming two consecutive labels for all pictures
1 Comment
Siddharth Bhutiya
on 24 May 2021
In your case the values are consecutive so you could extract them and use mean to calculate the mean. If you did not know the number of entires for each image and the values for each image was scattered around the table then a more general approach would be to use something like varfun and use "Label" as your grouping variable and @mean as your grouping function.
t =
10×2 table
Label MaxPosition
_________ ___________
"Image 3" 473
"Image 1" 418
"Image 2" 2
"Image 5" 41
"Image 5" 46
"Image 4" 467
"Image 5" 256
"Image 1" 342
"Image 1" 395
"Image 1" 123
>> t = varfun(@mean,t,'GroupingVariables','Label')
t =
5×3 table
Label GroupCount mean_MaxPosition
_________ __________ ________________
"Image 1" 4 319.5
"Image 2" 1 2
"Image 3" 1 473
"Image 4" 1 467
"Image 5" 3 114.33
1 Comment
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!