problems with intersect and cellfun functions

조회 수: 1 (최근 30일)
Mark Bodner
Mark Bodner 2018년 8월 6일
댓글: Mark Bodner 2018년 8월 8일
I'm using the script below to try to find all the matches in a column of 2 excel files. The intersect function returns indices which are out of bounds for the first cell (patternsize is 41351 x 1, dataP is 2000 x 1). The first cell of "ic" is 41352, and for "id" it is 6001. The first cell of D is then blank. When I try to pass all of this on in the next line, everything blows up and I get the error 'Index exceeds matrix dimensions'. The script seemed to work fine with other files with different dimensions. I'm pretty much a novice with this, so any suggestions as to what is going wrong is greatly appreciated.
[numdataP,patternsizeP]=xlsread('state.xlsx');
[~,dataP]=xlsread('pinellas.xlsx');
[D,ic,id] = intersect(patternsizeP,dataP)
indexP=cellfun(@(x)find(ismember(dataP,x)==1),D,'uniformoutput',false)
  댓글 수: 6
jonas
jonas 2018년 8월 7일
편집: jonas 2018년 8월 7일
I am the same person who worked with you previously, so I know the background.
The script works fine for me, probably because you did not include the entire data set. For some reason, xlsread grabs an empty row at the end of the excel-sheet when importing state.xlsx. That's why you get an empty cell. You should make sure to either not import this cell (either directly on import or remove it afterwards). As a first test, you can manually specify the range of cells to import from excel. I think this may solve your problem.
In addition, I believe you are only using the first column of patternsizeP so make sure to REMOVE all other columns (or better yet, don't import them). Right now you are jamming a bunch of unnecessary data into the intersect function, which is likely to cause issues.
If none of the above works, make sure to upload a segment of data for which the error is reproduced, so that we can find the bug.
I think dsb was referring to you cooperating by submitting data, don't worry about it :)
Mark Bodner
Mark Bodner 2018년 8월 8일
Thanks. Yes I realized that it's grabbing an empty cell at the end, but no matter what I do, it does so. The program seems to ignore whatever I import into the workspace and just goes for the files in the matlab folder. No matter what I do to these files or what I import, the empty cell still gets grabbed. I am using column 17 (and would like to use more) from patternsizeP, but I'll try removing a bunch of those columns to see what effect it has. Thanks again.

댓글을 달려면 로그인하십시오.

채택된 답변

Stephen23
Stephen23 2018년 8월 7일
편집: Stephen23 2018년 8월 7일
>> [~,~,rawS] = xlsread('state.xlsx');
>> [~,~,rawP] = xlsread('pinellas.xlsx');
>> [idxP,idxS] = ismember(rawP(:,1),rawS(:,1));
>> out = rawS(idxS(idxP),[1,1,16]);
>> out(:,2) = rawP(idxP,2)
out =
'PIN159614' 'SOO' [271]
'PIN159614' 'SO1' [271]
'PIN159614' 'SOA' [271]
'PIN159614' 'SO0' [271]
'PIN159614' 'SO3' [271]
'PIN159614' 'SO2' [271]
'PIN159614' 'SO5' [271]
'PIN159614' 'SOE' [271]
'PIN159614' 'SOD' [271]
'PIN159614' 'SOG' [271]
'PIN159614' 'SO6' [271]
'PIN725789' 'SOA' [262]
'PIN725789' 'SOC' [262]
'PIN725789' 'SOB' [262]
'PIN725789' 'SOE' [262]
'PIN725789' 'SOD' [262]
'PIN725789' 'SOG' [262]
'PIN725789' 'SOL' [262]
'PIN725789' 'SOO' [262]
'PIN725789' 'SO1' [262]
'PIN725789' 'SO0' [262]
'PIN725789' 'SO3' [262]
'PIN725789' 'SO2' [262]
'PIN725789' 'SO5' [262]
'PIN725789' 'SO7' [262]
'PIN725789' 'SO6' [262]
'PIN725789' 'SO8' [262]
  댓글 수: 5
Mark Bodner
Mark Bodner 2018년 8월 8일
Yes that is the name. The output of the command is C:\Users\mbodner\Downloads\MathWorks\R2017b\newscriptcode.m
The files 'pinellas' and 'state' are in the same location. The file is open and I'm just hitting the green "run" arrow.
Mark Bodner
Mark Bodner 2018년 8월 8일
Solved the problem. Foolishly copied over >> from your code in each line. Removed it and everything works great.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Environment and Settings에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by