How can I split two side-by-side vectors into many individual vector pairs?

I have several 10368x1 vectors, of which each of the 10368 pieces represents a value at a different location on a map. I would like to put two of them at a time side-by-side, and split them horizontally into 10368 separate 2x1 vectors, such that:
M = [ 1 3 5 7 9 ]
N = [ 0 2 4 6 8 ]
WANT:
MN1 = [ 1 0 ]
MN2 = [ 3 2 ]
MN3 = [ 5 4 ]
MN4 = [ 7 6 ]
MN5 = [ 9 8 ]
The end result should be a pair of different values at each location. Here's the kicker: since some of these vectors include NaN values, and I don't want to misplace any of the associated locations in the vectors, I would also like to return NaN values as placeholders for any incomplete pair of data, such that:
M = [ 1 3 NaN 7 9 ]
N = [ 0 2 4 6 NaN ]
WANT:
MN1 = [ 1 0 ]
MN2 = [ 3 2 ]
MN3 = NaN
MN4 = [ 7 6 ]
MN5 = NaN
How can I do this?

 採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 10 月 21 日
M = [ 1 3 NaN 7 9 ];
N = [ 0 2 4 6 NaN ];
idx=any(isnan([M;N]));
out=arrayfun(@(x,y) [x y],M,N,'un',0) ;
out(idx)={nan}

6 件のコメント

Patrick
Patrick 2013 年 10 月 21 日
That gives me a logical answer, not a vector answer; I need the actual values. When I used that, I got 10368 instances of "[1x2 double]", so it's close, but it seems like your code is simply telling me that the answers are 1x2 doubles, rather than giving me the values.
Azzi Abdelmalek
Azzi Abdelmalek 2013 年 10 月 21 日
編集済み: Azzi Abdelmalek 2013 年 10 月 21 日
You can check the values
celldisp(out)
You should read about cell array
help cell
If you want the first element
out{1}
Patrick
Patrick 2013 年 10 月 21 日
Thank you, I understand now.
Patrick
Patrick 2013 年 10 月 21 日
How can I create a cell array without removing the NaN values?
Azzi Abdelmalek
Azzi Abdelmalek 2013 年 10 月 21 日
What do you mean?
Patrick
Patrick 2013 年 10 月 21 日
Please see my new question. I have the cell array, but I need to perform a K-means analysis, and it is not working. I thought leaving the NaN values in might solve that problem, as the internal code for K-means in MATLAB automatically takes NaN values out of the equation, but I still have no luck.

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

その他の回答 (0 件)

カテゴリ

製品

質問済み:

2013 年 10 月 21 日

コメント済み:

2013 年 10 月 21 日

Community Treasure Hunt

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

Start Hunting!

Translated by