find a value & store in new variable

조회 수: 1 (최근 30일)
Ubu
Ubu 2012년 3월 7일
Hi,
Standard question:
We have a cell array A cell with integer and decimal numeric values:
A = {[1;2;29;2],[3;4;0.3;3]}
(this is just an example, of course)
We would like to find all occurrences of [2] in column 1, and save corresponded value in column 2 in a new variable "new_variable". In this case "new_variable" would contain [4] and [3], and we could make statistics on this new variable alone, without bothering about the rest.
find(ismember(A{1,1},[2]))
would of course just give us the row coordinates of column 1.
Any suggestion?
Thank you very much!

채택된 답변

TAB
TAB 2012년 3월 7일
new_variable = A{2}(find(ismember(A{1,1},[2])))

추가 답변 (2개)

Jan
Jan 2012년 3월 7일
It might be easier to use a matrix instead of a cell. But it works with your data representation also:
index = A{1} == 2;
new_variable = A{2}(index);
Or in one line:
new_variable = A{2}(A{1} == 2);
find and ismember are not needed here.
  댓글 수: 1
Ubu
Ubu 2012년 3월 19일
Hi Jan,
maybe you want to have a look at this, indeed.
Cheers,
Udiubu

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


Ubu
Ubu 2012년 3월 7일
Both work!
Thanks TAB, Thanks Jan.

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by