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

 Réponse acceptée

Andrei Bobrov
Andrei Bobrov le 4 Avr 2017

0 votes

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 commentaires

lucksBi
lucksBi le 4 Avr 2017
Thank you so much. This is exactly what i want. Can I get final result in cell array other than 3D array?
Yes.
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];
out = cellfun(@(x)a.*ismember((1:size(a,1))',x),b,'un',0);
lucksBi
lucksBi le 5 Avr 2017
Thanks alot
lucksBi
lucksBi le 5 Avr 2017
It is working fine on small matrix e.g i tried on 6x6 matrix size it is giving accurate results. But when i tried it on matrix of size 943x943, it gives following error. ??? Error using ==> times Matrix dimensions must agree.
Error in ==> @(x)a.*ismember((1:size(a,1))',x) Error in ==> file2 at 22 out=cellfun(@(x)a.*ismember((1:size(a,1))',x),b,'un',0);
can you please help on this.
Andrei Bobrov
Andrei Bobrov le 5 Avr 2017
Please attach your data that gives an error.
lucksBi
lucksBi le 5 Avr 2017
here are .mat files of a & b matrices
lucksBi
lucksBi le 17 Avr 2017
hey can you please help on this matter? i am unable to fix this issue with with given mat files.

Connectez-vous pour commenter.

Plus de réponses (1)

Thorsten
Thorsten le 4 Avr 2017

0 votes

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 commentaires

lucksBi
lucksBi le 4 Avr 2017
Thanks Alot. But it gives 4x2 array for b{1,1}, (changes no of columns in each cell) i want a 4x4 array with elements of rows that are member of b and other rows as zero.
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 le 4 Avr 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]

Connectez-vous pour commenter.

Catégories

Tags

Aucun tag saisi pour le moment.

Community Treasure Hunt

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

Start Hunting!

Translated by