Hi there, I am trying to average sections of a matrix at a time. The matrix is 31x109. Initially I am trying to build another matrix from the average of the first 25 elements increments in the first row, then the second 25(beginning from the the last element in the previous average); this will later be applied to all rows. My current attempt is
for f = 1:[1:25:108]; Ave_PpIX(f,:) = PpIX_Dose_Matrix(1,1:f); end
ave_PpIX = mean(Ave_PpIX);
Am idea of the matrix is: [ave(1-25) ave(13-38) ave(25-50)...]
If anyone has had any previous experience doing this I would really appreciate any help.
Thnaks Luke

댓글 수: 1

Jan
Jan 2018년 7월 12일
Why 108 in the loop, not 109? What should happen with the last chunk, which has less then 25 elements?
1:[a:b:c] is the same as 1:a.

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

 채택된 답변

Star Strider
Star Strider 2018년 7월 12일

0 개 추천

I am not certain that I understand the result you want.
Try this:
M = rand(31, 109); % Original Matrix
Cols = [ones(1, fix(size(M,2)/25))*25 rem(size(M,2),25)]; % Create Column Sub-Sections
C = mat2cell(M, size(M,1), Cols); % Cell Array Of Sub-Sections
Cmean = cellfun(@(x)mean(x,2), C, 'Uni',0); % Row Means For Each Section
Mmean = cell2mat(Cmean); % Convert To Double Matrix Of Row Mean Vectors For Each Sub-Section

댓글 수: 9

Luke McLellan
Luke McLellan 2018년 7월 13일
Thank you very much.
My pleasure.
‘I have just realised ... that the matrix values should be [1-25 13-38 25-50...]...’
You can easily implement this in my code by changing the ‘Cols’ vector, and segment the matrix as many times as necessary to get all the segments you want.
For example one line would be:
Cols1 = [25 25 ...];
C1 = mat2cell(M, size(M,1), Cols1);
and a second call:
Cols2 = [12 26 ...]; % The Second Segment Will Be Columns 13-38
C2 = mat2cell(M, size(M,1), Cols2);
The code becomes a bit more complicated with the additional segmentation steps. It is still relatively straightforward and efficient. The column dimensions in the ‘Cols1’ and ‘Cols2’ vectors each have to total the column size (dimension 2) of your matrix, 109 here.
Luke McLellan
Luke McLellan 2018년 7월 16일
Thanks again. Still need to figure out the matrix segregation for Cols2.
As always, my pleasure.
If you only want columns 13-38, this works:
Cols2 = [12 26 size(M,2)-(12+26)];
The second segment of the mat2cell result is the one you want.
To be certain ‘Cols2’ (or any other segmentation vector) will work in mat2cell, ‘sum(Cols2)’ has to equal ‘size(M,2)’. (All elements must of course be integers.)
Luke McLellan
Luke McLellan 2018년 7월 16일
I had it so that I was repeating 3 iterations of 13-38 rather than following the trend but your code, I believe, follows the sequencing? Am I correct in saying that?
Luke McLellan
Luke McLellan 2018년 7월 16일
No, sorry. It doesn't continue the sequence.
The 13-38 segment is 26 elements, so to repeat 3 consecutive segments, ‘Cols2’ becomes:
Cols2 = [12 26 26 26 size(M,2)-(26*3+12)];
that becomes ‘[12 26 26 26 19]’, in mat2cell creating segments from columns 1-12. 13-38, 39-64, 65-90, and 91-109. So here, use segments 2, 3, and 4 out of the 5 segments created.
Luke McLellan
Luke McLellan 2018년 7월 17일
I do believe that you are my knight in shining algorithm but I will have to return to the code next week now
Star Strider
Star Strider 2018년 7월 17일
Thank you!

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

추가 답변 (1개)

Jan
Jan 2018년 7월 12일
편집: Jan 2018년 7월 13일

0 개 추천

PpIX_Dose_Matrix = rand(31, 109);
n = size(PpIX_Dose_Matrix, 2);
k = 0;
for f = 1:24:n
k = k + 1;
Ave_PpIX(k) = mean(PpIX_Dose_Matrix(1, f:f+24));
end
This can fail, when f+25 is outside the existing range. A test with if can catch this:
if f+25 <= n
...

댓글 수: 3

Luke McLellan
Luke McLellan 2018년 7월 13일
편집: Luke McLellan 2018년 7월 13일
Thank you very much. I have implemented it. I have just realised (probably through my tiredness yesterday) that the matrix values should be [1-25 13-38 25-50...] so time to work on this haha.
Jan
Jan 2018년 7월 13일
for f = 1:12:n
k = k + 1;
Ave_PpIX(k) = mean(PpIX_Dose_Matrix(1, f:f+24));
end
Luke McLellan
Luke McLellan 2018년 7월 16일
Thanks again for your comments

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

카테고리

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

질문:

2018년 7월 12일

댓글:

2018년 7월 17일

Community Treasure Hunt

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

Start Hunting!

Translated by