searching row indices in a matrix

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 Comments

Your statements are incompatible. your variable a is (1) empty ( I have a vector a = [] ), (2) a column vector ( of size (n,1)), and (3) something that cannot be ( a(1) = [2 3 4 5]) at the same time ... ????
I suggest you rephrase your question and give clearer examples of the input and the output.
I have a vector a of size (n,1) I have another matrix B which is made is of size (m,2). Vector a is inside a loop which keeps changing its values. Here a1, a2,a3... are vectors of the type "a" as described above
Eg. a1 = [2 3 4 5]; a2= [5 6 7 8]; a3 = [8 9 10 11] B(:,1) = [a1 a2 a3];
a4= [23 24 25 26]; a5 = [26 27 28 29] ;a6 = [29 30 31 32] B(:,2) = [a4 a5 a6];
Now,I want the row indices in B which correspond to a vectors.
For eg. if I check if a4 is a member of B, I want output as [1 2 3 4] a5 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 a2 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.
@Chaitanya Sanghavi: It appears that you are using notation from some other language, because what you have written makes little sense as MATLAB code. For example this:
B(:,1) = [a1 a2 a3];
would allocate the RHS to the first column of array B. Assuming that everything is numeric then this would only work without error if the RHS is a vector, in which case why bother with the distraction of defining three sub-vectors which are simply concatenated into one long vector? Instead of this complex code:
>> a1 = [2 3 4 5]; a2= [5 6 7 8]; a3 = [8 9 10 11]; [a1 a2 a3]
ans =
2 3 4 5 5 6 7 8 8 9 10 11
All you need is
>> [2 3 4 5 5 6 7 8 8 9 10 11]
ans =
2 3 4 5 5 6 7 8 8 9 10 11
Or do you (incorrectly) believe that you can put vectors into individual elements of numeric array B ? What size do you expect array B to have?
Your problem is likely quite simple, and it would be simpler to help you if you showed us actual MATLAB syntax.
Well, a1, a2 and a3 are all vectors. I also have a4,a5,a6 as vectors. I basically want the same indices when I want a1 or a4 from a big array. In this case the big array is B. Thus, I store the entries a1 and a4 in the same rows of the array B similarly for a2,a5 and a3,a6 resp.
But there are common elements in a1 and a2. As a result the row indices always acquire a lower index.
I tried something like ismember and find and combinations of this but it does not work. B has 2 columns and the same no. of rows as [a1 a2 a3].
You are right with the assumption that everything is a vector in "RHS". Well, This works syntax works in Matlab. :)

Answers (2)

Stephen23
Stephen23 on 22 Dec 2017
Use strfind if you want to preserve order. It will return the first index of each location where the substring is found in a longer string, and yes it works with numeric vectors too!
John BG
John BG on 4 Jan 2018
Edited: John BG on 4 Jan 2018
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

This question is closed.

Asked:

on 22 Dec 2017

Closed:

on 20 Aug 2021

Community Treasure Hunt

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

Start Hunting!