I have a 1×4 cell array of
{60×4 double} {60×4 double} {60×4 double} {60×4 double}
and I need to;
-find the values at the 3rd column at the rows have "20" at the first column in each cell
-then eliminate zeros if there is any
-then take average
As an illustration
10 NaN 0 NaN 20 NaN 623 1
10 NaN 0 NaN 20 NaN 0 NaN
20 NaN 310 1 10 NaN 38 1
20 NaN 0 NaN 10 NaN 0 NaN
20 NaN 1445 1 10 NaN 0 NaN
the first cell should give the average of 310 and 1445, whereas the second cell should give 623.
I am thinking of a for loop but I couldnt make it work. Thanks in advance!

3 件のコメント

Adam Danz
Adam Danz 2020 年 6 月 23 日
Could you share your loop so we can help fix it? Otherwise we're doing it for you.
Ali Yilmaz
Ali Yilmaz 2020 年 6 月 23 日
Hi Adam,
Honestly I deleted my version as it was not even close to what I need. I'll be glad if you can share your version.
Thanks and regards
Adam Danz
Adam Danz 2020 年 6 月 25 日
See answer below.

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

 採用された回答

Adam Danz
Adam Danz 2020 年 6 月 24 日
編集済み: Adam Danz 2020 年 6 月 25 日

0 投票

If you want to do this in a loop, there are 4 steps I've outlined below.
"c" is the 1x4 cell array.
column3Means = nan(size(c));
for i = 1:numel(c)
% Find rows where column 1 equals 20
index1 = c{i}(:,1) == 20;
% Find rows where column 3 is not 0
index2 = c{i}(:,3) ~= 0;
% Combine those indices
idx = index1 & index2;
% Use the combined index to compute the mean
column3Means(i) = mean(c{i}(idx,3),'omitnan');
end
FYI, the loop can be avoided using the cellfun() function.

10 件のコメント

madhan ravi
madhan ravi 2020 年 6 月 25 日

Wow didn’t know there was a cellarray() function , was it released in 2020a?

Adam Danz
Adam Danz 2020 年 6 月 25 日
oops :D typo. I meant cellfun.
Corrected. Thanks, madhan.
Ali Yilmaz
Ali Yilmaz 2020 年 6 月 25 日
Hi Adam,
Thank you very much for your response.
When I apply your code, column3means turns out to be [NaN,NaN,NaN,NaN]
Ali Yilmaz
Ali Yilmaz 2020 年 6 月 25 日
Here I added the cell array, now I have 1x5 cell array. But the question is the same.
-find the values at the 3rd column at the rows have "20" at the first column in each cell
-then eliminate zeros if there is any
-then take average
Adam Danz
Adam Danz 2020 年 6 月 25 日
Could you share the code you're using to compute the mean?
You probably just need to work in the "nanflag" input to ignore nans in your data.
Ali Yilmaz
Ali Yilmaz 2020 年 6 月 25 日
編集済み: Adam Danz 2020 年 6 月 25 日
my cell array is zeroback
column3Means = nan(size(zeroback));
for i = 1:numel(zeroback)
% Find rows where column 1 equals 20
index1 = zeroback{i}(:,1) == 20;
% Find rows where column 3 is not 0
index2 = zeroback{i}(:,3) ~= 0;
end
mean(index2, 'omitnan')
Now I have a mean value of 0.25.
But actually I need the rows which have 20 at 1st column whilst the value in their 3rd column is nonzero. However, with this code above I feel like I have the rows that have 20 at their first coulmn and the rows that have a nonzero value at their 3rd column separately.
Adam Danz
Adam Danz 2020 年 6 月 25 日
You're not using index1. You're also taking the mean of the index rather than using the index to extract the values from zeroback.
But please look at every line and look at the values being produced on each line so you can understand what they are doing.
Try this.
column3Means = nan(size(c));
for i = 1:numel(c)
% Find rows where column 1 equals 20
index1 = c{i}(:,1) == 20;
% Find rows where column 3 is not 0
index2 = c{i}(:,3) ~= 0;
% Combine those indices
idx = index1 & index2;
% Use the combined index to compute the mean
column3Means(i) = mean(c{i}(idx,3),'omitnan')
end
Ali Yilmaz
Ali Yilmaz 2020 年 6 月 25 日
Thank you very much Adam;
that worked perfectly, appreciate your help
Adam Danz
Adam Danz 2020 年 6 月 25 日
Glad it worked out.
The take-home message is: learn/use indexing.
Indexing is a superpower of Matlab. My answer uses logical indexing but there are other types of indexing as well.
Ali Yilmaz
Ali Yilmaz 2020 年 6 月 25 日
Thank you so much again, I will definitely go throuh the link you've shared.

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeCell Arrays についてさらに検索

質問済み:

2020 年 6 月 23 日

コメント済み:

2020 年 6 月 25 日

Community Treasure Hunt

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

Start Hunting!

Translated by