How can I combine values from different columns of a matrix into one single column?

Hello,
I have a matrix with three columns and many rows. The first column is the count. There are values in some sections of the second and third columns that I am not interested in. However, they do not overlap. There is no single row for which I want to extract values of both columns. I want to combine the values I am interested in in one single column, either a new column or in one of those two columns.Initially, I assigned NaN to those cells that I am not interested and then I was trying to use a loop but I am new to Matlab so it does not work. Any help will be very much appreciated. Trying to this manually in Excel takes a long time! Thanks!

 採用された回答

Leah
Leah 2013 年 3 月 8 日
it would be helpful to see you code so far, but i think this is what you need. A is your original matrix (nx3)
Anew=A(:,2);
index1=isnan(Anew);
Anew(index1,1)= A(index1,3)
you could do this quickly in Excel by using an "if" statement
=IF(ISNA(B1),C1,B1)

1 件のコメント

Kemal
Kemal 2013 年 3 月 8 日
Thank you for showing me the Excel function, too!

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

その他の回答 (2 件)

Matt J
Matt J 2013 年 3 月 8 日
Index the rows of column 3 that you want and then do a vectorized assignment. If A is your matrix and keeprows are the rows of column 3 that you want to keep,
newcol=A(:,2);
newcol(keeprows)=A(keeprows,3);

3 件のコメント

Kemal
Kemal 2013 年 3 月 8 日
Sorry. Could you help a little more? I have NaNs in sporadic sections of column 3. Those are the ones I do not want. And the ones I want have values in them and conversely those rows are assigned NaNs in column 2. Thank you.
newcol=A(:,2);
keeprows=~isnan(A(:,3));
newcol(keeprows)=A(keeprows,3);
Kemal
Kemal 2013 年 3 月 8 日
Thank you. It did it!

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

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 3 月 8 日
編集済み: Azzi Abdelmalek 2013 年 3 月 8 日
EDIT
A=[1 4 6;2 nan 14;3 25 nan;4 78 77]
B=A
idx2=find(isnan(A(:,2)))
idx3=find(isnan(A(:,3)))
B(idx2,2)=A(idx2,3)
B(idx3,3)=A(idx3,2)

2 件のコメント

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 3 月 8 日
編集済み: Azzi Abdelmalek 2013 年 3 月 8 日
But the problem is, when values in col2 and col3 are not nan, which one will you choose?
Kemal
Kemal 2013 年 3 月 8 日
You are right although it wasn't in my case. That would be the next question. Thank you!

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

カテゴリ

質問済み:

2013 年 3 月 8 日

Community Treasure Hunt

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

Start Hunting!

Translated by