cutting a matrix A into 3 pieces with respect to matrix B

1 回表示 (過去 30 日間)
Mori
Mori 2015 年 7 月 17 日
編集済み: Mori 2015 年 7 月 17 日
here we have :
A=[1 2 3 14 15 16 7 8]
B=[1 2 3]
we know location of B is over 14 15 16 of matrix A.
we need to get 3 different matrix as below:
c=[1 2 3]
d=[14 15 16]
e=[7 8]
whar are the commands? many thanks in advance
  13 件のコメント
Walter Roberson
Walter Roberson 2015 年 7 月 17 日
Is it known that those entries in B are bit-for-bit copies of the entries in A? Such as if A was indexed to produce B? Because when two floating point values are computed by even very very slightly different code, the results will not necessarily be equal due to floating point round-off. If the entries are not bit-for-bit copies then you will need to compare with a tolerance rather than compare for equality.
Mori
Mori 2015 年 7 月 17 日
I can manage to get column 2 and 3 of A and B exactly equal, i.e. bit-for-bit copies. tried your answer but got error message. lets take into account that Columns 2 and 3 is equal in both matrix.

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

採用された回答

Walter Roberson
Walter Roberson 2015 年 7 月 17 日
[tf, idx] = ismember(B, A, 'rows');
minidx = min(idx);
maxidx = max(idx);
c = A(1:minidx-1,:);
d = A(minidx:maxidx,:);
e = A(maxidx+1:end, :);
  2 件のコメント
Mori
Mori 2015 年 7 月 17 日
got this error
Mori
Mori 2015 年 7 月 17 日
編集済み: Mori 2015 年 7 月 17 日
just changed 'rows' to 'legacy' and it worked. Thank you
[tf, idx] = ismember(B, A, 'legacy');
minidx = min(idx);
maxidx = max(idx);
c = A(1:minidx-1,:);
d = A(minidx:maxidx,:);
e = A(maxidx+1:end, :);

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeResizing and Reshaping Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by