Loop through tables in a cell array

I have two 41x1 cell arrays 'DS_10B' and 'DS_10T' containing 41 different tables each. The tables contain stock return data from column 4 onwards. (find the data attached)
First I want to loop through all tables of the array 'DS_10B' and calculate the mean stock return. I don't care about the mean return per table or column but only need the overall mean return of all the combined data in the array.
Then I want to repeat the exact same procedure for the other array 'DS_10T'.
I already managed to calculate the mean of one single table's stock return data with this:
% Extract test table
t = DS_10B{1,1};
% Extract relevant columns of test table
% Get all columns with stock return data of table as a column vectors of doubles
lastColumns = t{:,4:end};
% Reshape into one single column in order to calculate mean
lastCo = reshape(lastColumns,[],1);
% Mean the last column but use omitnan
mean_table = mean(lastCo,'omitnan');
Would be delighted if someone could help me with the for loop.
Thanks a lot in advance!

 采纳的回答

Managed to solve it myself! :)
Here's my solution for everyone with a similar problem:
%% Mean Bottom Portfolio
meanDS_10B = NaN(1,numel(DS_10B));
for k =1:numel(DS_10B)
t = DS_10B{k,:};
% Extract stock data columns of every table
lastColumn = t{:, 4:end}; % Get all columns with stock return data of table as column vectors of doubles
%reshape into one single column in order to calculate mean
lastCo = reshape(lastColumn,[],1);
% mean the last column but use omitnan
mean_column = mean(lastCo,'omitnan');
meanDS_10B(k) = mean_column;
end
% Average all Means
avgRET_Bottom = mean(meanDS_10B,'omitnan');

3 个评论

I never would have (and obviously never did) understand that solution was what you wanted.
For reference, the previous thread to this is Extract monthly stock return data out of a table .
No problem man! You helped me with a lot of my previous steps. I appreciate that a lot.
And once again apologies for the misunderstandings. I honestly did my best in describing my problem, but with english not being my native language and me not being that familiar with Matlab yet, I understand that it can be a bit difficult to understand at times... :)
No worries!
My problem is that although I was active in the U.S. stock market many years ago (out for more than a decade), I never did any technical analysis, and since economics and such is far from my areas of expertise, had no idea what you wanted. The MATLAB-related stuff was straightforward, everything else, not so much.

请先登录,再进行评论。

更多回答(0 个)

类别

在 帮助中心 和 File Exchange 中查找有关 Multidimensional Arrays 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by