Find the index of cell array containing numeric values
조회 수: 3 (최근 30일)
이전 댓글 표시
Hi!I have a cell array=YTLim, which contains at each cell the measurements of each for significant wave height Hs(numeric values).Each cell has 2920=365*8 or 2928=366*8 measurements,it depends on the year.So first cell has the values
YTLim{1}=[1,2,3,...,2920]
YTLim{2}=[2921,2922,2923,...,5840] etc
I have,also, a vector that contains the spot of some measurements. For example:
maxpos=[242;365;...;8555]
I need to find a way to take the index of the cell containing the vector's value...Is that possible?
I need something like that:
YS=[1;1;2;...;10;11;11]
댓글 수: 0
채택된 답변
the cyclist
2014년 4월 17일
편집: the cyclist
2014년 4월 17일
Here is a miniature example that I believe does what you've asked for:
YTLim{1}=[1,2,3,4];
YTLim{2}=[5,6,7,8];
YTLim{3}=[9,10,11,12,13];
maxpos=[1,3,7,13];
c = cellfun(@(x)(ismember(maxpos,x)),YTLim,'UniformOutput',false);
[YS,~] = find(reshape([c{:}],numel(maxpos),[])')
I have to admit the code is compact and maybe a bit obscure. But if you take the time to examine each piece, I'm confident you can see what each individual command is doing.
댓글 수: 5
Stephen23
2014년 12월 21일
편집: Stephen23
2015년 1월 2일
James: If the data arrays are contained in a cell array, then there you basically have two choices for comparing these with some scalar value:
- in a loop
- using cellfun
The loop may be a bit faster, but cellfun can produce much neater code.
Some other points in your code need improving though:
Note that because you are comparing two scalars there is no reason to use ismember at all: you can use logical relational operators for a scalar with any sized array:
idx = Array == Scalar;
If you are so concerned about speed why do you call randi 10,000 times in a loop? This indicates some reading up on MATLAB vectorization is required. For example, it could be generated faster and tidier using num2cell:
num2cell(randi(10,100,1))
This leads on to the most important question: why are you storing scalar values in a cell array? Unless you have a good reason to do this, you should keep your data in a numeric array, which then makes all the other operations suddenly much, much easier to perform... For example, all of your code could be reduced to:
A = randi(10,100,1);
B = A==3;
James
2014년 12월 28일
The only problem is that I have uneven arrays of different sizes. Hence the need for cells. However, using the logical == operator instead of the ismember function definitely gives better results. In fact it cut the time by about 3/4. I'm a little embarrassed I didn't think of it. Thank you for helping me out.
I'm actually writing my own Q-hull algorithm. This is for determining if a point is in the outside set of the current faces. I know MATLAB has its own Q-hull method built into the delaunayTriangulation function but I wanted to make my own.
Thanks, James Peters
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!