How to read 3 values in row of 3 columns as 1
3 次查看(过去 30 天)
显示 更早的评论
I have 2 columns as input with each column having 3 sub-columns i.e
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/158010/image.jpeg)
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 个评论
Walter Roberson
2016-11-14
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?
采纳的回答
Guillaume
2016-11-15
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 个评论
更多回答(3 个)
KSSV
2016-11-14
If C is your 3x3 matrix, then you can call any row or column as one entity. Like C(1,:) etc.
0 个评论
Walter Roberson
2016-11-14
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 个评论
Vaclav Rimal
2016-11-15
编辑:Vaclav Rimal
2016-11-15
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Text Files 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!