フィルターのクリア

How to group variable of interests by logical indexing?

1 回表示 (過去 30 日間)
Sonisa
Sonisa 2016 年 3 月 2 日
コメント済み: Sonisa 2016 年 3 月 2 日
I have this sort of data. I want to plot PAW with Pasture 9 Patch 1, Pasture 9 with Patch 3, along with Pasture 9 with Patch 5. I want to use logical indexing as, Pasture (Pasture==9), Patch(Patch==1) and then retrieve PAW data based on it. I tried this,
for ii =1:length(PAW)
Pasture_index = find(Pasture==9);
PAW40pasture(ii) = mean(PAW40(Pasture_index,1));
end
But this did not work. Any suggestions/help will be appreciated.
Pasture Patch PAW
9 1 59.90366347
9 1 58.71485861
9 1 57.35723603
9 1 57.36212973
9 3 57.04769458
9 3 127.7284367
9 3 143.7897127
9 3 143.5349223
9 3 142.5156813
9 5 142.3629269
9 5 148.7025447
9 5 149.7302998
9 5 147.5149873
9 5 88.55115557
9 5 80.52936618
  5 件のコメント
Sonisa
Sonisa 2016 年 3 月 2 日
all are double. Three column and 16 rows.
Sonisa
Sonisa 2016 年 3 月 2 日
Image Analyst, I sent you my file. Please have a look on it

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

採用された回答

Guillaume
Guillaume 2016 年 3 月 2 日
First, you're not using logical indexing in your code because of the find. The exact same code without the call to find (i.e. Pasture_index = Pasture==9;) would be using logical indexing.
It's not clear to me why you thought the snippet you wrote would produce the result you want since you never filter for a given patch value.
If all you want is to produce the mean of PAW for identical combinations of pasture and patch then you don't need a loop and just need accumarray:
[ppval, ~, rows] = unique([Pasture, Patch], 'rows'); %assuming that Pasture and Patch are column vectors
PAW40pasture = accumarray(rows, PAW40(:, 1), [], @mean);
out = array2table([ppval, PAW40pasture], 'VariableNames', {'Pasture', 'Patch', 'mean'}) %this line just for pretty display
If you wanted to plot them (against what x axis?) then you could do it with a loop as follows:
figure; hold on;
ppvalues = unique([Pasture, Patch]);
for ppvalue = ppvalues'
inpasturepatch = Pasture == ppvalue(1) & Patch == ppvalue(2); %This is logical indexing. No find
plot(PAW40(inpasturepatch, 1), 'DisplayName', sprintf('Pasture %d, Patch %d', ppvalue(1), ppvalue(2)));
end

その他の回答 (1 件)

Image Analyst
Image Analyst 2016 年 3 月 2 日
Do you have the stats toolbox? It looks like your data is in a table. You might be able to get the mean for all numbers very easily in one line of code with grpstats:
statarray = grpstats(tbl,groupvar)
  6 件のコメント
Image Analyst
Image Analyst 2016 年 3 月 2 日
You accepted an answer and said thanks, so I'm assuming you got everything working and this doesn't apply anymore.
Sonisa
Sonisa 2016 年 3 月 2 日
I have attached data and I want to group data by Pasture 9, patch 1 and date, Pasture 9, Patch 3 and date, and Pasture 9, Patch 5 and date. I used the grpstats to create groupvar. But it is giving me data as the original file which is 1795*4 dimension. Actually i want it to be 598*4 dimension. Hope it makes sense to you.
Thanks so much.

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

カテゴリ

Help Center および File ExchangeInteractive Control and Callbacks についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by