Accessing multiple columns in cell array
조회 수: 3 (최근 30일)
이전 댓글 표시
Hi,
I need to calculate the mean of 5 measurements. The 5 measurements are presented in a 5*1 cell array. The variable is called Data.
Thus, I need the mean of:
Data{1, 1}(:,end)
Data{2, 1}(:,end)
Data{3, 1}(:,end)
Data{4, 1}(:,end)
Data{5, 1}(:,end)
How do I acces all 5 columns at once?
Thanks in advance.
-Lasse
댓글 수: 0
답변 (3개)
Scott MacKenzie
2021년 6월 9일
편집: Scott MacKenzie
2021년 6월 9일
You need to retrieve the elements in their underlying data type (hence the use of braces) and concatenate the values using brackets:
mean([Data{:,1}])
If indeed thare are only 5 values (measurements), then you don't need ",1". Just use
mean([Data{:}])
댓글 수: 0
Stephen23
2021년 6월 9일
Fake data:
Data = arrayfun(@(n)randi(9,2,3),1:5,'uni',0).'; % fake data
Data{:}
Method one:
M = mean(cat(3,Data{:}),3);
M = M(:,end)
Method two:
M = mean(cell2mat(reshape(Data,1,1,[])),3);
M = M(:,end)
Method three:
M = mean(cell2mat(cellfun(@(m)m(:,end),Data.','uni',0)),2)
Method four:
M = mean(cell2mat(arrayfun(@(n)Data{n}(:,end),1:numel(Data),'uni',0)),2)
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!