find equal value into cell
古いコメントを表示
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 件のコメント
fn='https://www.mathworks.com/matlabcentral/answers/uploaded_files/1364343/workspace_cell_value.mat';
load(fn)
whos
Unfortunately, either I don't know how to access a .mat file within the forum or it can't be done with default .mat files...
Star Strider
2023 年 4 月 23 日
@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’.
.
採用された回答
その他の回答 (1 件)
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 件のコメント
Greg LANGE
2023 年 4 月 23 日
"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.
カテゴリ
ヘルプ センター および File Exchange で Matrix Indexing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!