Indexing to create a new array

1 回表示 (過去 30 日間)
Kate
Kate 2013 年 11 月 11 日
コメント済み: Marc 2013 年 11 月 12 日
I have a very simple question, but just can't seem to wrap my brain around the answer today.
My index:
>> Wtr
Wtr =
12
1
2
My data:
mNEE =
1.0000 NaN
2.0000 NaN
3.0000 0.2165
4.0000 3.9982
5.0000 18.3253
6.0000 32.6099
7.0000 14.7678
8.0000 36.8627
9.0000 -1.0730
10.0000 -8.2807
11.0000 6.7998
12.0000 25.0182
1.0000 8.1078
2.0000 0.7304
3.0000 -6.2877
4.0000 -2.5207
5.0000 10.5052
6.0000 11.2116
7.0000 4.8839
8.0000 -8.1560
9.0000 -16.8618
10.0000 -45.3915
11.0000 -39.8423
12.0000 -8.4954
1.0000 13.1454
2.0000 31.7136
3.0000 41.5338
4.0000 19.9113
5.0000 18.7724
6.0000 -20.1884
7.0000 -7.9781
8.0000 -12.7571
9.0000 -21.3081
10.0000 -18.9930
11.0000 -31.7573
12.0000 -7.2601
1.0000 10.2586
2.0000 16.4023
3.0000 45.1588
4.0000 24.6945
5.0000 0.0275
6.0000 7.3914
7.0000 -3.5258
8.0000 -15.4096
9.0000 -19.4229
10.0000 -21.2903
11.0000 -11.5092
12.0000 -55.0183
1.0000 -53.4840
2.0000 -4.5846
3.0000 14.0066
4.0000 0.5472
5.0000 31.4220
6.0000 8.1386
7.0000 -13.0516
8.0000 -2.7136
9.0000 NaN
10.0000 NaN
11.0000 NaN
12.0000 NaN
All that I really want to do is pull data where the 1st column of mNEE==Anything in Wtr.
But when I run this super simple for loop I only get the first 3 matches, when I want all the data that falls within the category of Wtr in a new array:
for i=1:length(mNEE)
t=mNEE(Wtr,:);
end
>> t
t =
12.0000 25.0182
1.0000 NaN
2.0000 NaN
Could someone please help me understand why Matlab finds the first 3 matches, but not anymore? This seems so simple but is eluding me.
Thanks a million.

採用された回答

Image Analyst
Image Analyst 2013 年 11 月 11 日
Try this:
matchingRows = ismember(mNEE(:,1), Wtr)
extractedRows = mNEE(matchingRows, :)
  3 件のコメント
Kate
Kate 2013 年 11 月 11 日
And I checked to see if my Matlab was getting cranky, but it can find simple ismembers:
test =
1
2
3
4
5
6
7
8
9
10
>> t=[2,4,6]'
t =
2
4
6
>> ismember(test, t)
ans =
0
1
0
1
0
1
0
0
0
0
Kate
Kate 2013 年 11 月 11 日
Nevermind, this works perfectly, thanks :)

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

その他の回答 (1 件)

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 11 月 11 日
編集済み: Azzi Abdelmalek 2013 年 11 月 11 日
mNEE =[1.0000 NaN
2.0000 NaN
3.0000 0.2165
4.0000 3.9982
5.0000 18.3253
6.0000 32.6099]
wtr=[12;1;2]
idx=ismember(mNEE(:,1),wtr)
mNEE(idx,:)=[]
%or
data=mNEE(~idx,:)
  5 件のコメント
Azzi Abdelmalek
Azzi Abdelmalek 2013 年 11 月 12 日
What do you mean?
Marc
Marc 2013 年 11 月 12 日
Just that you are RIGHT and Kate is WRONG.... Let it begin

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

カテゴリ

Help Center および File ExchangeData Distribution Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by