필터 지우기
필터 지우기

cell to matrix

조회 수: 1 (최근 30일)
ricco
ricco 2011년 11월 3일
I have a data set which is in a cell structure (<1x20>) I want to place all of the data into one matrix. As each of the data sets in this cell structure is of different size, such as the first one is: 61169x15 and the second is 59529x15 and so on... i'm trying to find a way of combining the cells into a matrix. I can do this manually by:
new_data=[data1;data2;data3;data4]
But i would like to run the same script for other data sets therefore am looking for a way of doing this in a loop. Is that possible? So, I was thinking of a loop which ran though the cells i.e. for i=1:size(data,2) %=1:20
and then applying the same process as above in the loop, but im not sure on how I would go about doing this.
thanks
  댓글 수: 1
Jan
Jan 2011년 11월 4일
Please by careful with the term "structure". It is often confused with "STRUCT" array.

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

채택된 답변

Jan
Jan 2011년 11월 4일
If all cell elements have teh same number of columns:
new_data = cat(1, data{:});
  댓글 수: 3
Jan
Jan 2011년 11월 4일
@ricco: This is not possible. If all the arrays in the cell have the same number of columns, they can be concatenated using CAT(1, C{:}) or CELL2MAT(C'). If this does not work for your problem, the number of columns must be different. Please check this again:
cellfun('size', data, 2)
ricco
ricco 2011년 11월 4일
It works now, thank you very much for your help. The problem was that the data wasn't cleaned by who ever recovered it so one of the arrays had a different number of columns (14 not 15) due to an instrument malfunction. Which I probably wouldn't have spotted without your comment above.
thanks again

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

추가 답변 (2개)

Fangjun Jiang
Fangjun Jiang 2011년 11월 3일
If all cells have the same number of columns, would this help?
a{1}=rand(3,2);
a{2}=rand(4,2);
a{3}=rand(5,2);
cell2mat(a')
  댓글 수: 2
ricco
ricco 2011년 11월 4일
In principle it should work because its basically the same as you have shown but for some reason it doesn't. I receive an error:
??? Error using ==> cat
CAT dimensions are not consistent
does it make a difference if mine are 'double' cells?
Jan
Jan 2011년 11월 4일
@ricco: No, the type of the cell elements does not matter - btw.: Fangjun's data are DOUBLEs also. But the dimensions must be matching. While "cell2mat(a')" works, "cell2mat(a)" must fail.

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


Ora Zyto
Ora Zyto 2011년 11월 3일
From your description, it seems like all of your data sets have the same number of columns, and varying number of rows. In your for loop, append your current dataset to your existing matrix:
for i=1:length(data),
new_data=[new_data;data{i}]
end
Ora
  댓글 수: 3
Jan
Jan 2011년 11월 4일
@Ora Zyto: The loop let the array grow in each iteration. This is very inefficient.
Ora Zyto
Ora Zyto 2011년 11월 4일
@Jan Simon: You are totally right. I didn't think about the CAT function :-)

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

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by