Finding row indexes in array

1 visualización (últimos 30 días)
lucksBi
lucksBi el 4 de Abr. de 2017
Comentada: lucksBi el 17 de Abr. de 2017
I have an array
a=[0 0 1 0 ; 1 2 3 0; 1 0 3 4; 0 2 0 0 ]
& cell array like
b{1,1}=[4;3]
b{2,1}=[3;1;4]
I want to find elements of cell array in a. For example: for b{2,1} 1st, 3rd and 4th row of a should be displayed and 2nd should be zero.
Thanks in advance

Respuesta aceptada

Andrei Bobrov
Andrei Bobrov el 4 de Abr. de 2017
a=[0 0 1 0 ; 1 2 3 0; 1 0 3 4; 0 2 0 0 ];
b{1,1}=[4;3];
b{2,1}=[3;1;4];
v = 1:size(a,1);
t = cell2mat(cellfun(@(x)ismember(v,x),b,'un',0))';
out = a.*permute(t,[1,3,2]);
  7 comentarios
lucksBi
lucksBi el 5 de Abr. de 2017
here are .mat files of a & b matrices
lucksBi
lucksBi el 17 de Abr. de 2017
hey can you please help on this matter? i am unable to fix this issue with with given mat files.

Iniciar sesión para comentar.

Más respuestas (1)

Thorsten
Thorsten el 4 de Abr. de 2017
idx = cellfun(@(x) any(ismember(a, x)), b, 'UniformOutput', false);
rows = cellfun(@(x) a(:, x), idx, 'UniformOutput', false);
Or in one line:
rows = cellfun(@(x) a(:, any(ismember(a, x))), b, 'UniformOutput', false);
  3 comentarios
Thorsten
Thorsten el 4 de Abr. de 2017
Like this?
idx = cellfun(@(x) any(ismember(a, x)), b, 'UniformOutput', false);
a0 = zeros(size(a));
for i = 1:numel(idx)
rows{i} = a0;
rows{i}(:,idx{i}) = a(:,idx{i});
end
lucksBi
lucksBi el 4 de Abr. de 2017
result should be like: result{1,1}= [0,0,0,0;0,0,0,0;1,0,3,4;0,2,0,0] result{1,2}= [0,0,1,0;0,0,0,0;1,0,3,4;0,2,0,0]

Iniciar sesión para comentar.

Categorías

Más información sobre Matrices and Arrays en Help Center y File Exchange.

Etiquetas

Aún no se han introducido etiquetas.

Community Treasure Hunt

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

Start Hunting!

Translated by