ismember error message "Subscript indices must either be real positive integers or logicals."

1 visualización (últimos 30 días)
I have two matrices A and B, and the desired output in an attached csv file. To obtain the desired outcome I have run
[~,loc] = ismember(A(:,1),B(:,1));
CC = [A(:,1),B(loc,2)];
But I got an error message
Subscript indices must either be real positive integers or logicals.
Please advise.
  2 comentarios
Paolo
Paolo el 3 de Jun. de 2018
In the .csv file you provided, each value in A has its own cell. However for data in B, you have multiple values in single cells. Is this intended? I noticed using readtable to read the .csv file.

Iniciar sesión para comentar.

Respuestas (1)

monika shivhare
monika shivhare el 3 de Jun. de 2018
Editada: monika shivhare el 3 de Jun. de 2018
[Lia,Locb] = ismember(X,Y)
Determines which elements of X are also in Y as well as their corresponding locations in B. Locb is array of size(X), with value at Locb(i) as location of value X(i) in array Y if X(i) is present in Y else zero.
Since some elements of A(:,1) are not present in B(:,1), loc contains some zero elements.In question above, loc=[1,2,4,0,0,0,0,0,0,0,0]. When using B(loc,2) you are trying to use all the elements of loc as index for B. In MATLAB, only positive integer index are allowed, hence it is showing error.
If you want to access values in 2nd column of B at rows where elements of A(:,1) is a member of B(:,1) you can use:
[~,loc] = ismember(A(:,1),B(:,1));
b=B(nonzeros(loc),2)
Now again, you cannot concatenate A(:,1) and b because A(:,1) is 11*1 while b is 3*1 .
Hence CC= [A(:,1),b]; will cause an error "Dimensions of matrices being concatenated are not consistent.".If you want to make it a column, then use
CC= [A(:,1);b];
If you want to concatenate A(:,1) with a array with value B(second column) where A(1st column) values are present in B(1st column), we have to pad other values with zeros and than concatenate them row-wise to make a 11*2 matrix.
[n,~]=size(A);
b=zeros(n,1);
for i=1:n
if(loc(i))
b(i)=B(loc(i),2);
end
end
CC=[A(:,1),b];

Categorías

Más información sobre Structures en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by