Hi there;
I have a vector A(kx1) and a matrix B (nxk). I need select rows from that correspond to ONES in A, and sum those rows mod2:
Example:
A =[1 0 1 0 1] and
B = [1 0 1 0 1;
1 0 0 1 0;
0 1 1 1 1;
1 1 0 1 1;
0 1 1 0 1];
In this example, I want to add rows that indicates ones in A, i.e. rows of B(1, 3,5) & sum those 3 rows mod2.
Kindly help.

 採用された回答

Star Strider
Star Strider 2016 年 11 月 27 日

0 投票

I’m not certain that I understand what you want to do.
See if this works:
A = [1 0 1 0 1];
B = [1 0 1 0 1; 1 0 0 1 0; 0 1 1 1 1; 1 1 0 1 1; 0 1 1 0 1];
C = B(logical(A'),:);
Out = mod(sum(C,2),2)

4 件のコメント

Star Strider
Star Strider 2016 年 11 月 27 日
Moe Joe’s ‘Answer’ moved here
A = [1 0 1 0 1];
B = [1 0 1 0 1; 1 0 0 1 0; 0 1 1 1 1; 1 1 0 1 1; 0 1 1 0 1];
C = B(logical(A'),:);
Then I complete it as:
D=sum(C(:,1:n));
Result =[1 2 3 1 3];
Then E=mod(D,2);
E=[1 0 1 1 1];
Kindly check it, and if there is a better way to do it, then show me.
Star Strider
Star Strider 2016 年 11 月 27 日
The better way is my code!
At least it is if I read your Question correctly (and I believe I did).
This is specifically because in your Question you wrote ‘i.e. rows of B(1, 3, 5)’.
Your code creates 5 column sums, not the 3 row sums you described.
My code produces:
Out =
1
0
1
Note that these are the 3 row sums mod(,2) that you said you wanted.
This seems to me to be the result desired.
Moe Joe
Moe Joe 2016 年 11 月 27 日
OK, let me rephrase my question in terms of a picture:
Star Strider
Star Strider 2016 年 11 月 27 日
That clarifies things. You are actually summing the columns of ‘D’, so that would be ‘sum(D,1)’ (or more simply, ‘sum(D)’) rather than ‘sum(D,2)’ that would sum the rows.
So the desired result would be:
Out = mod(sum(D),2)
or in my code:
Out = mod(sum(C),2)
producing the result described by (5).

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeProgramming についてさらに検索

製品

質問済み:

2016 年 11 月 27 日

コメント済み:

2016 年 11 月 27 日

Community Treasure Hunt

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

Start Hunting!

Translated by