find equal value into cell
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Greg LANGE
el 23 de Abr. de 2023
Comentada: Star Strider
el 24 de Abr. de 2023
I try to find the line from table " vehicule2021bis2" with "Num_Acc" corresponding with the same "Num_Acc" in the "lieux2021bis3"
Then I copy certain colon from "lieux2021bis3" into " vehicule2021bis2"
I tried
Wanted = find(vehicule2021bis2.Num_Acc{1}==lieux2021bis3.Num_Acc{:}))
I wrote instead a if with a for with while and iF, which take very long time to
for i=1 :970000
clc
i
line=0;
lol1=height(lieux2021bis3)
j=1;
while j <=lol1
if vehicule2021bis2.Num_Acc{i}== lieux2021bis3.Num_Acc{j}
vehicule2021bis2.catr(i)=lieux2021bis3.catr(j);
vehicule2021bis2.voie(i)=lieux2021bis3.voie(j);
break
else
j=j+1;
end
end
I am convinced to use find or another method will be much more faster.
Same question but with 2 condition
Wanted = find(vehicule2021bis2.Num_Acc{1}==lieux2021bis3.Num_Acc{:}&& vehicule2021bis2.id_vehicule{1}==lieux2021bis3.id_vehicule{:} ))
2 comentarios
Star Strider
el 23 de Abr. de 2023
@dpb — This is relatively new (within the last couple weeks).
Left-click on the ‘paperclip’ icon, left-click on ‘Link from this thread’ then choose the file and left-click on ‘Submit’. It automatically attaches the selected file to that particular Answer or Comment, then you can use load or any of the others (fileread, readtable, etc.), however it is still necessary to provide the file name. (I right-click on the file name where it’s originally posted, choose ‘Copy link’, then paste it in the appropriate place in the function argument list and edit out everything other than the file name. It’s easier than typing it in most instances, and prevernts misspellings.)
To upload a file from your computer with the ‘paperclip’ icon open, left-click on ‘Choose a file’.
.
Respuesta aceptada
Star Strider
el 23 de Abr. de 2023
I am not certain what references you want, so it may be necessary to reverse the order of the arguments in the ismember call here —
LD = load('workspace_cell_value.mat');
vehicule2021bis2 = LD.vehicule2021bis2
lieux2021bis3 = LD.lieux2021bis3
% cell2mat(vehicule2021bis2{:,1})
% cell2mat(vehicule2021bis2{:,3})
% cell2mat(lieux2021bis3{:,1})
Lv1 = cellfun(@(x)~isempty(x),lieux2021bis3{:,2})
cell2mat(lieux2021bis3{Lv1,2})
Wanted_1a = ismember(cell2mat(vehicule2021bis2.Num_Acc), cell2mat(lieux2021bis3.Num_Acc)) % Single Variable
Wanted_1b = ismember(cell2mat(vehicule2021bis2{Lv1,[1 3]}), cell2mat(lieux2021bis3{Lv1,[1 2]}), 'rows') % Combined Variables
Wanted_1 = find(Wanted_1a)
Wanted_2 = ismember(cell2mat(lieux2021bis3.Num_Acc), cell2mat(vehicule2021bis2.Num_Acc))
% vehicule2021bis2.catr(Wanted_2)=lieux2021bis3.catr(Wanted_2)
% vehicule2021bis2.voie(Wanted_2)=lieux2021bis3.voie(Wanted_2)
Wanted_2 = find(Wanted_2)
I am not certain where to go on the rest of this.
.
7 comentarios
Star Strider
el 24 de Abr. de 2023
LD = load('workspace_cell_value.mat');
vehicule2021bis2 = LD.vehicule2021bis2
lieux2021bis3 = LD.lieux2021bis3
Wanted_1a = ismember(cell2mat(vehicule2021bis2.Num_Acc), cell2mat(lieux2021bis3.Num_Acc)); % Single Variable
Result = vehicule2021bis2(Wanted_1a,:)
format long
vehic_Num_Acc = cell2mat(lieux2021bis3.Num_Acc)
lieux_Num_Acc = cell2mat(lieux2021bis3.Num_Acc)
Wanted_2 = ismember(cell2mat(lieux2021bis3.Num_Acc), cell2mat(vehicule2021bis2.Num_Acc)) % Are Any Elements Of vehicule2021bis2.Num_Acc' Found In 'lieux2021bis3.Num_Acc'?
Only 1, and that is:
Result = lieux2021bis3(Wanted_2,:)
So, nothing is wrong. It is simply that only the first element of ‘lieux2021bis3.Num_Acc’ matches any elemnts of ‘vehicule2021bis2.Num_Acc’ and specifically:
Check1 = cell2mat(lieux2021bis3.Num_Acc) - cell2mat(vehicule2021bis2.Num_Acc).'
Check2 = - cell2mat(vehicule2021bis2.Num_Acc(4)) - cell2mat(lieux2021bis3.Num_Acc)
So there is only a match between the first 2 elements of ‘vehicule2021bis2.Num_Acc’ and ‘lieux2021bis3.Num_Acc’ and none of them match ‘vehicule2021bis2.Num_Acc(4)’.
That is simply how comparisons work.
.
Más respuestas (1)
Stephen23
el 23 de Abr. de 2023
Editada: Stephen23
el 23 de Abr. de 2023
Forget about loops and FIND and ISMEMBER and other fiddling around, you should be using OUTERJOIN command. If your data are nice and tidy, then one single command is all you need. The OUTERJOIN command will be fast.
But because your data are messy (e.g. scalar/empty numeric in cell arrays), you need to tidy up the data first. For example, convert the cell array of numeric to actual numeric data, and then get rid of the missing data rows (even better: do this when you import the data).
format long G
S = load('workspace_cell_value.mat');
L = S.lieux2021bis3;
V = S.vehicule2021bis2;
L = rmmissing(convertvars(L,{'Num_Acc','id_vehicule'},@mydouble))
V = rmmissing(convertvars(V,{'Num_Acc','id_vehicule'},@mydouble))
W = outerjoin(V,L, 'keys','Num_Acc', 'RightVariables',{'catr','voie'}, 'Type','left')
function out = mydouble(inp)
out = nan(size(inp));
idx = cellfun(@isscalar,inp);
out(idx) = [inp{idx}];
end
2 comentarios
Stephen23
el 23 de Abr. de 2023
Editada: Stephen23
el 23 de Abr. de 2023
"what the purpose of the following code ? (I don't understand it)"
It helps convert the cell array of scalar/empty numerics into a simple numeric vector: it creates a NaN array exactly the same size as the cell array and copies the scalar data into it. The function is used as an input to CONVERTVARS.
Note that OUTERJOIN will likely be much faster than anything else you try.
Ver también
Categorías
Más información sobre Matrix Indexing en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!