Info
この質問は閉じられています。 編集または回答するには再度開いてください。
searching row indices in a matrix
1 回表示 (過去 30 日間)
古いコメントを表示
I have a vector a = [] ; of size (n,1) I have another matrix B which is made is of size (m,2).
Eg. a(1) = [2 3 4 5]; a(2) = [5 6 7 8]; a(3) = [8 9 10 11] B(:,1) = [a(1) a(2) a(3)];
a(4) = [23 24 25 26]; a(5) = [26 27 28 29] ;a(6) = [29 30 31 32] B(:,2) = [a(4) a(5) a(6)];
Now,I want the row indices in B which correspond to a vectors.
For eg. if I check if a(4) is a member of B, I want output as [1 2 3 4] a(5) is a member of B, I want output as [5 6 7 8] and not [4 6 7 8];
Also the same when I check a(2) for B. The output should be [5 6 7 8] and not [4 6 7 8].
I can of course do this using loops and if conditions, but I want to use smart indexing commands.
5 件のコメント
回答 (2 件)
John BG
2018 年 1 月 4 日
編集済み: John BG
2018 年 1 月 4 日
Hi Chaitanya
to meet the requirement
I want the row indices in B which correspond to a vectors.
you may want to consider using cells instead.
Start defining your data like this:
a{1} = [2 3 4 5]
a{2} = [5 6 7 8]
a{3} = [8 9 10 11]
and
B = [a(1) a(2) a(3)];
B is now a cell with 3 components, each a vector of your choice
to add rows to B, for instance
B=[B;B]
B =
2×3 cell array
[1×4 double] [1×4 double] [1×4 double]
[1×4 double] [1×4 double] [1×4 double]
the thing is, if you try to use ismember now, MATLAB returns
Error using cell/ismember (line 34)
Input A of class cell and input B of class cell must be cell arrays of character
vectors, unless one is a character vector.
so you may need a custom function to perform the checks mentioned.
if you find this answer useful would you please be so kind to consider marking my answer as Accepted Answer?
To any other reader, if you find this answer useful please consider clicking on the thumbs-up vote link
thanks in advance for time and attention
John BG
0 件のコメント
この質問は閉じられています。
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!