remove elements from cell

조회 수: 9 (최근 30일)
skysky2000
skysky2000 2017년 7월 12일
댓글: skysky2000 2017년 7월 19일
Dear all, I know, I've asked alot and I really appreciate your help. I have a cell array:
a = {[1 2 3 55 77 8] [3 77] [ 1 2 5 7 8 9 0 2] [ 3 44 6]}
and I would like to remove the numbers before the index based on vector b=[3 9 6]. The expected answer should be:
cell2 = {[55 77 8] [77] [0 2] []}
Thanks.
  댓글 수: 1
Jan
Jan 2017년 7월 13일
편집: Jan 2017년 7월 13일
It is very welcome if you ask many questions here. But it would be appreciated, if you explain the problem as detailed as needed to define it uniquely. The explanation "remove the numbers before the index based on vector b=[3 9 6]" is not clear. Then showing any own effort and asking a specific question concerning your code would be useful also, for you and for the readers.

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

답변 (2개)

Image Analyst
Image Analyst 2017년 7월 12일
This will do it:
a = {[1 2 3 55 77 8] [3 77] [ 1 2 5 7 8 9 0 2] [ 3 44 6]}
b=[3 9 6]
% cell2 = {[55 77 8] [77] [0 2] []}
for k = 1 : length(a)
thisArray = a{k}; % Extract array from cell.
[inA, inB] = ismember(b, thisArray); % Find where any of b occurs in thisArray.
index = find(inB>0, 1, 'last'); % Find last occurrence.
outputArray = thisArray(inB(index)+1:end); % Extract out to new array.
cell2{k} = outputArray; % Stuff into a new cell array.
end
celldisp(cell2); % Display in command window.
It's easy and intuitive, though I'm sure someone will give you a cryptic one-liner shortly.
  댓글 수: 3
dbmn
dbmn 2017년 7월 13일
skysky could you tell us what didnt work in your case?
image_analyst: one liner coming up
cell2 = cellfun(@(x) x(max([find(ismember(x, b),1, 'last'), 0])+1:end), a, 'uni', 0);
p.s. when you use max([index, 0]) you can get rid of the empty matrices :)
skysky2000
skysky2000 2017년 7월 19일
thanks alot dbmn for your help

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


skysky2000
skysky2000 2017년 7월 13일
any help please?
  댓글 수: 2
Image Analyst
Image Analyst 2017년 7월 13일
I gave you help. My code produced exactly the cell array that you said you wanted. What's the problem? Prove to me that it does not give you what you asked for, because I don't know what else to do.
Jan
Jan 2017년 7월 13일
@skysky2000: Please do not bump your thread by posting a pseudo-answer. The question is not really clear, but Image Analysts has posted a method to produce the requested answer. Now it is your turn to explain, which details is not solved yet.

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

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by