필터 지우기
필터 지우기

Create a Matrix from a cell array

조회 수: 4 (최근 30일)
Thimiod Athan
Thimiod Athan 2016년 9월 22일
편집: Stephen23 2016년 9월 22일

Hello

I want to create a matrix from a cell with the below:

cell(1,1)='Value,Rooms,Cleanliness,Service'	  
cell(1,2)='2 of 5 stars,2 of 5 stars,4 of 5 stars,2 of 5 stars' 
cell(2,1)='Value,Location,Sleep Quality,Rooms,Cleanliness,Service'   
cell(2,2)='5 of 5 stars,5 of 5 stars,5 of 5 stars,5 of 5 stars,5 of 5 stars,5 of 5 stars' 
cell(3,1)='Location,Rooms,Cleanliness,Service'	cell(3,2)='4 of 5 stars,1 of 5 stars,3 of 5 stars,4 of 5 stars' 

etc

I want to combine each characteristic for example location with each rating for example in cell(2,2) is 5 in cell(3,2) is 4. This ratings should be at the same column. If the rating doesn't appear for example the location in the first raw the matrix will take the number 10.

Do you have any ideas?

Thank you.

  댓글 수: 1
Stephen23
Stephen23 2016년 9월 22일
편집: Stephen23 2016년 9월 22일
This might be answered best by improving how this data is imported into MATLAB, as textscan or importdata might be able to do this quite easily. Are those data imported from a file?

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

채택된 답변

Guillaume
Guillaume 2016년 9월 22일
Your post is not very clear, not help with the fact that your example does not use valid matlab syntax. Possibly, that's what you want:
ratings = {'Value,Rooms,Cleanliness,Service', '2 of 5 stars,2 of 5 stars,4 of 5 stars,2 of 5 stars'; ...
'Value,Location,Sleep Quality,Rooms,Cleanliness,Service', '5 of 5 stars,5 of 5 stars,5 of 5 stars,5 of 5 stars,5 of 5 stars,5 of 5 stars'; ...
'Location,Rooms,Cleanliness,Service', '4 of 5 stars,1 of 5 stars,3 of 5 stars,4 of 5 stars'}
%The above is not a very useful way to store data, so split it:
ratinglabels = cellfun(@(r) strsplit(r, ','), ratings(:, 1), 'UniformOutput', false);
ratingscores = cellfun(@(r) str2double(regexp(r, '\d+(?= of)', 'match')), ratings(:, 2), 'UniformOutput', false); %extract score
%get unique labels, and mapping between original array and label column:
[labels, ~, destinationcolumn] = unique([ratinglabels{:}]);
%get row destination for each score:
destinationrow = repelem(1:size(ratings, 1), cellfun(@numel, ratingscores))';
%create output matrix and fill with score. Default value if score is missing is NaN:
scores = nan(size(ratings, 1), numel(labels));
scores(sub2ind(size(scores), destinationrow, destinationcolumn)) = [ratingscores{:}];
%pretty display:
scores = array2table(scores, 'VariableNames', matlab.lang.makeValidName(labels))
%export to excel:
writetable(scores, 'someexcelfile.xlsx');

추가 답변 (1개)

Image Analyst
Image Analyst 2016년 9월 22일
A 2-D cell array (like you have) is a matrix. It's a matrix of cells. Just define your cells differently if you want the things in between the commas to be in different cells.
  댓글 수: 1
Thimiod Athan
Thimiod Athan 2016년 9월 22일
편집: Thimiod Athan 2016년 9월 22일
My mistake, I want to create a matrix in excel. The cell is like the picture

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

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by