# How to read 3 values in row of 3 columns as 1

9 views (last 30 days)

Show older comments

I have 2 columns as input with each column having 3 sub-columns i.e

I want to read the values [1 30 31] in column 1 as 1 value or entity & same for column 2 the values [4501 4502 4503] as 1. In the same way i want to read the remaining values in rows of both columns as one so the i can make possible combinations between two columns.

##### 3 Comments

Walter Roberson
on 14 Nov 2016

### Accepted Answer

Guillaume
on 15 Nov 2016

Here is a generic way of creating all the combinations of all the rows of a (reasonable) arbitrary number of matrices.

%input: a row vector cell array of matrices, e.g.:

matrices = {[1 30 31;33 1071 1072;35 1079 1077], [4501 4502 4503;4568 5287 5286;4566 5295 5291]};

assert(isrow(matrices), 'Cell array is not a row vector'); %otherwise the cell2mat call on the last line will not work properly

rows = cellfun(@(m) 1:size(m, 1), matrices, 'UniformOutput', false); %generate vectors of row indices for each matrix

[rows{:}] = ndgrid(rows{:}); %generate combinations of the index vectors

combinedrows = cell2mat(cellfun(@(m, r) m(r(:), :), matrices, rows, 'UniformOutput', false))

This will work for any size of matrices and up to a reasonable number of these matrices (or ngdgrid will error if there are two many)

##### 0 Comments

### More Answers (3)

KSSV
on 14 Nov 2016

If C is your 3x3 matrix, then you can call any row or column as one entity. Like C(1,:) etc.

##### 0 Comments

Walter Roberson
on 14 Nov 2016

You will need to read as numeric and then use mat2cell (or plain expression indexing) to do the grouping you want.

None of the import tools support the kind of grouping you want to do.

The closest would be if it were a text file and there were a delimiter (comma or tab) between columns and the gap shown in the diagram is really there, then you could use textscan with a format such as

'%f%f%f%s%f%f%f'

and use CollectOutput, 1 . The CollectOutput would see the three %f in a row and would group them, then have the %s isolated because it is a different data type, then group the three %f. You would then throw away the empty string column.

But it's probably easier just extract the groups you need, like num2cell(TheInput(:,1:3))

##### 0 Comments

Vaclav Rimal
on 15 Nov 2016

Edited: Vaclav Rimal
on 15 Nov 2016

Suppose your two matrices you want to combine are:

A=[1 30 31;33 1071 1072;35 1079 1077];

B=[4501 4502 4503;4568 5287 5286;4566 5295 5291];

You should prepare Atmp by replicating the rows of A using repmat and reshape (note the transpositions because reshape works along columns; alternatively, a for cycle could be employed):

Atmp=repmat(A,1,size(B,1));

Atmp=reshape(Atmp(:)',size(A,2),[])';

And, finally, concatenate this auxiliary matrix with B replicated in vertical direction according to the number of rows of A:

result=[Atmp repmat(B,size(A,1),1)];

##### 0 Comments

### See Also

### Categories

### Community Treasure Hunt

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

Start Hunting!