How to considering another column in this code?

1 回表示 (過去 30 日間)
BN
BN 2020 年 6 月 2 日
コメント済み: Ameer Hamza 2020 年 6 月 2 日
Dear all, I use this code to get an average of certain columns namely rrr24 row-by-row for all tables inside a cell:
C1 = mean(cell2mat(cellfun(@(t) (t.rrr24),C1,'UniformOutput',false)),2);
Now I want to develop this code for considering another column namely PRECIP in it. I do it like this:
C1_1 = mean(cell2mat(cellfun(@(t) (t.rrr24),C1,'UniformOutput',false)),2);
C1_2 = mean(cell2mat(cellfun(@(t) (t.PRECIP),C1,'UniformOutput',false)),2);
C1 = [C1_1 C1_2];
But in order to summarize my code; is it possible to do this in one line?
  3 件のコメント
BN
BN 2020 年 6 月 2 日
編集済み: BN 2020 年 6 月 2 日
Dear Ameer Hamza, I add a small sample of my data here; And here is the code that I use now to gain an answer, which gains me desire output.
C1_1 = mean(cell2mat(cellfun(@(t) (t.rrr24),sample,'UniformOutput',false)),2);
C1_2 = mean(cell2mat(cellfun(@(t) (t.PRECIP),sample,'UniformOutput',false)),2);
C1 = [C1_1 C1_2];
But I wanna know is it possible to summarize these three lines in just one line?
Thank you so much
Ameer Hamza
Ameer Hamza 2020 年 6 月 2 日
Converting it to a single line might be possible, but that will likely make the statements quite complex.
A different question might be: do you want to do it for this specific case of two columns, or do you want to extend it to multiple columns without the need to write each line separately.

サインインしてコメントする。

採用された回答

Daniel Abajo
Daniel Abajo 2020 年 6 月 2 日
Hello Behzad...
I dont like new matlab objects like table...nothing better than bulk of numbers... sometimes in single column better, all togheter in ram...
well my solution for you.... all means in 1 way...
load('clc')
sample2=cellfun(@(x) table2array(x),sample,'unif',false);
mean([sample2{:}],1)
ans =
Columns 1 through 11
0.4765 0.4842 0.4854 0.5023 0.5172 0.4840 0.5117 0.5423 0.4865 0.5054 0.4968
Column 12
0.4622
  1 件のコメント
Daniel Abajo
Daniel Abajo 2020 年 6 月 2 日
Sorry, i made a mistake, i assumed that you wanted historic means in row.... :s... if you want means between samples, as i see in your example... this is the code, just concatenate the samples in 3rd dimension and make the mean in 3rd dimension...
load('clc')
% Vectorize
sample2=cellfun(@(x) table2array(x),sample,'unif',false);
sample2=mean(cat(3,sample2{:}),3);
>> isequal(C1,sample2(:,[3,1]))
ans =
logical
1

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCell Arrays についてさらに検索

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by