フィルターのクリア

Extracting sections of data from a Matrix with Indexing

2 ビュー (過去 30 日間)
Jenny
Jenny 2013 年 9 月 26 日
コメント済み: Jenny 2013 年 9 月 30 日
I have a 84761 x 8 matrix (see attached file as an example of data from the matrix).
I would like to extract all of the rows of data where the month (3rd column) = 11, 12, 1, 2 and 3.
I have set the 'months' to look for in an array called WinterMonths.
I am trying to loop through the matrix (EditedWindTempMatrix) and write all of the data where the month = a 'winter' month to a new matrix.
The 'for loop' loops through the 5 'winter' months.
My problem lies in coding the if loop. i.e. extract the data and write it to a new matrix if the condition is true.
How do I index the new matrix (called WinterWTMatrix) to write the data from the matrix with all the data (EditedWindTempMatrix) to the new matrix only when the condition specified is true?
WinterMonths = [11 12 1 2 3]; % 1 x 5 array
for k = 1:length(WinterMonths);
MonthIdx=WinterMonths(k);
if EditedWindTempMatrix(:,3)==MonthIdx;
WinterWTMatrix=EditedWindTempMatrixdata;
end
end
I know that I am not correctly extracting the data from the EditedWindTempMatrix and not correctly writing it to the WinterWTMatrix but I am not sure how to do this.
(I can do it column by column but wanted a more efficient way to extract the desired data).

採用された回答

Image Analyst
Image Analyst 2013 年 9 月 26 日
編集済み: Image Analyst 2013 年 9 月 26 日
Jenny:
Use ismember:
% Create sample data:
%EditedWindTempMatrix = randi(12, 10,3)
% Define which months are winter.
WinterMonths = [11 12 1 2 3]; % 1 x 5 array
% Find out which rows are winter.
rowsToExtract = ismember(EditedWindTempMatrix(:, 3), WinterMonths)
% Get those rows from the original matrix
WinterData = EditedWindTempMatrix(rowsToExtract,:)
  1 件のコメント
Jenny
Jenny 2013 年 9 月 30 日
Thank you for this. It makes it look easy. Jenny

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

その他の回答 (1 件)

Simon
Simon 2013 年 9 月 26 日
編集済み: Simon 2013 年 9 月 26 日
Hi!
Search the third column for your desired month
irows = (EditedWindTempMatrix(:,3) == 11) | ...
(EditedWindTempMatrix(:,3) == 12) | ...
(EditedWindTempMatrix(:,3) == 1) | ...
(EditedWindTempMatrix(:,3) == 2) | ...
(EditedWindTempMatrix(:,3) == 3);
Then "irows" is a logical array of length of size(1) of EditedWindTempMatrix. Then
WinterWTMatrix = EditedWindTempMatrix(irows, :);
  3 件のコメント
Simon
Simon 2013 年 9 月 26 日
Yes, 'or' ('|') instead of 'and' ('&'). You're right. I corrected it above.
Your solution looks better than mine. But I experience with "ismember" a slower execution, especially if I use it with full 2d arrays and not with just a column like in this example. But that's another problem.
Jenny
Jenny 2013 年 9 月 30 日
Thank you for your help. Jenny

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

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by