find a value & store in new variable
2 views (last 30 days)
Show older comments
Hi,
Standard question:
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!
0 Comments
Accepted Answer
More Answers (2)
Jan
on 7 Mar 2012
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.
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!