Cell array to meet conditions and cell indexing

I apologise in advance for incase this is unclear.
I have an array, with X Y columns and another column that identifies the other X and Ys that have met a previous condition. For example A=[16x1 double 16x1 double 8; 12x1 double 12x1 double []; 15x1 double 15x1 double [13,15]..... up to 19 rows]. If the condition was not met there is no match and so the third column reads [], as given for A(2,:). If two sets of x and y coordinates have been matched, then these can be found in rows 13 and 15 for example of column 1, for A(3,:).
I want to therefore have an output of the original X and Y sets, the matching rows (I already have) and the matching X Y coordinates. I had had numerous errors since there are empty cells, which I do not want to delete. Also, the number of matches changes each time, so concatenating brings errors.
Thankyou.

2 Comments

Please at least give us the arrays to work with. Edit your question, click the paperclip button, then both Choose file and Attach file buttons.
Thankyou, so here I have my X and Y coordinates in the first two columns. In the third i have the row numbers that match the condition i imposed. For example, for the first row, I would like to join the X Y coordinates for this row with that of the 8th row, as they have met the match. Similarly, for the third row the X Y coordinates have made a match with rows 13 and 15 so, I would like to join the X and Y for the 3rd, 13th and 15th row together, etc. Thankyou.

Sign in to comment.

 Accepted Answer

I'm not really clear on the format of the output you want, but the following should give you an idea what to do:
C = {[1:10]', [21:30]', 8; [41:45]', [51:55]', [2 4]; [60:70]', [80:90]', []} %demo data
C(:, 4) = cellfun(@(x, y, row) [x(row), y(row)], C(:, 1), C(:, 2), C(:, 3), 'UniformOutput', false)
celldisp(C(:, 4))

5 Comments

Thankyou, this helped me a little. So for this example, for the first row, rather than outputting the 8th value of C(1,1) and the 8th value of C(1,2) in C(1,4), I would like the output to be the C(8,1) and C(8,2) , I know in this example C has only 3 rows, so it wouldn't find the 8th row data but in my full example (I addeded the .mat file to my question) it would find this data set. Similarly for the second row, in your example we have [2 4], so i would ask for the output for C(2,4) to be the data C(2,1) C(2,2) AND C(4,1) C(4,2). These can be concatenated (they don't have to stay distinct) so there is not a problem with the consistently sized cells for example. Thanks again for your time.
If I understood correctly, this is probably what you want:
XY = cellfun(@(rows) cell2mat(X_Y_Match(rows, [1 2])), X_Y_Match(:, 3), 'UniformOutput', false)
Or as a loop:
XY = cell(size(X_Y_Match, 1), 1);
for rowidx = 1:size(X_Y_Match, 1)
rows = X_Y_Match(rowidx, 3);
XY{rowidx} = cell2mat(X_Y_Match(rows, [1 2]));
end
If you don't want the vectors concatenated just remove the cell2mat call in either option.
The first one is great, the loop gives an error Function 'subsindex' is not defined for values of class 'cell'. For the first solution, is there a way to seperate the X and Y, so I have the XY output as two columns, the first with all the Xs so nx1 double, and the next column as the corrosponding Ys as nx1 double as opposed to x2 doubles?
Thankyou!
Thankyou,I have figured this out!
The loop solution should have read:
rows = X_Y_Match{rowidx, 3};
Sorry about the typo.

Sign in to comment.

More Answers (0)

Categories

Asked:

on 22 Feb 2016

Commented:

on 23 Feb 2016

Community Treasure Hunt

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

Start Hunting!