matlab cell array indexing

조회 수: 5 (최근 30일)
Manu Mensa
Manu Mensa 2019년 4월 2일
편집: Adam Danz 2019년 4월 2일
Hello guys. Please forgive me if my question seems unclear as i am stiill finding my way through MATLAB. Say, i have a very large matrix , say M=rand(10,3).
M =
0.4464 0.4270 0.5464
0.1115 0.5055 0.2758
0.8828 0.0410 0.4191
0.7399 0.6778 0.9463
0.0032 0.8740 0.1323
0.1920 0.7142 0.4295
0.1379 0.5208 0.9257
0.0943 0.5353 0.8351
0.0390 0.7384 0.9540
0.6558 0.7057 0.4356
I also have some other matrixes which are subsets of the first and second column of M only i.e
C1 =
0.1115 0.5055
0.8828 0.0410
C2 =
0.1379 0.5208
0.0943 0.5353
How do i index into M to extract the third column of M that is equaivalent to elements of C1. i.e expected output should be:
new_C1 =
0.1115 0.5055 0.2758
0.8828 0.0410 0.4191, .
new_C2 =
0.1379 0.5208 0.9257
0.0943 0.5353 0.8351
Also if C1,C2,...Cn are in a cell array, how can i use a for loop to obtain new_C1 and new_C2.
Many thanks

채택된 답변

Guillaume
Guillaume 2019년 4월 2일
I'm assuming your C1 and C2 are not actual numbered variables but elements of a cell array, in which case:
newC = cellfun(@(c) M(ismember(M(:, [1 2]), c, 'rows'), :), C, 'UniformOutput', false);
Or as a loop:
newC = cell(size(C))
for idx = 1:numel(C)
c = C{idx};
newC{idx} = M(ismember(M(:, [1 2]), c, 'rows'), :);
end
This also assumes that the elements of C originally started as elements of M. If the elements of M and C have been obtained separately, e.g, by mathematical operations, it may well be that the 0.8828 in M is not exactly the same as the 0.8828 in M (but the difference is so small that you don't see it at the displayed precision). If that assumption is broken you will want to use ismembertol instead of ismember. The 'rows' option of ismember becomes 'ByRows', true with ismembertol.
  댓글 수: 1
Manu Mensa
Manu Mensa 2019년 4월 2일
Yes, Guillaume. C are elements of the cell array and started as actual elements of M. Thank you for the various options you have given me.
Thank you, Adam and Madhan. Your suggestions also worked. And to think i have spent the better part of the day on this problem!!
You guys are really awesome.
Thanks, once again!!

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

추가 답변 (1개)

Adam Danz
Adam Danz 2019년 4월 2일
편집: Adam Danz 2019년 4월 2일
[EDIT]: This only works with matrices (not cell arrays). I didn't notice the cell array term in the title of the question.
Use ismember() with 'rows' specification to search for C1 within the first two columns of M. That produces a logical index of rows that match C1 which can be used to pull out valules in column 3.
C1 = [C1, M(ismember(M(:,[1:2]), C1, 'rows'),3)];
C2 = [C2, M(ismember(M(:,[1:2]), C2, 'rows'),3)];
Note that if a row in C1 exists more than once in M, this simple method will cause an error (as will other solutions provided here).

카테고리

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