Clear Filters
Clear Filters

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

11 views (last 30 days)
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.
Arslan F
Arslan F on 14 Nov 2016
I want to make combinations between values of 2 main columns (if there would be 2 columns instead of 6 it would be easy i have already done it). I made above combinations of excel manually just as am example but my data is much larger then this and it would be impossible to do it manually.
Walter Roberson
Walter Roberson on 14 Nov 2016
Could you give examples of combinations? For example are you looking to create vectors in which the first three entries are [1 30 31] and the second three entries are all of the unique rows from columns 4 to 6, and then the same thing for [33 1071 1072] and all the unique rows from columns 4 to 6?

Sign in to comment.

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)

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.

Walter Roberson
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
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))

Vaclav Rimal
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):
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)];


Community Treasure Hunt

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

Start Hunting!