converting from cell 2 matrix

i have a cell like
a{1,1}={160;45;58;89}% in the form of 1*50char a{2,1}={160;45;58;89}% in the form of 1*50char a{3,1}={160;45;58;89}% in the form of 1*50char a{4,1}={160;45;58;89}% in the form of 1*50char
like this i have a cellsize of 50000 rows(a{50000,1})
what is the best way to convert elements inside cell to matrix form
eg o/p : desired matrix to be
b(1,:)= 160 45 58 89 b(2,:)= 160 45 58 89 b(3,:)= 160 45 58 89 b(4,:)= 160 45 58 89
problem here is the forloop is taking lot of time to convert to serired matrix way

 Respuesta aceptada

Jan
Jan el 15 de Nov. de 2012
Editada: Jan el 15 de Nov. de 2012

0 votos

I do not believe that the FOR loop is slow. If you post the code you are using, we will most likely find another problem. Without seeing the code, guessing the reasons is impossible.
testData = cell(1, 1e6);
testData(:) = {'12; 123; 45; 45; 0.1'};
dummy = sscanf(testData{1}, '%g;', [1, Inf]); % To get the width
width = numel(dummy);
Result = zeros(width, numel(testData)); % Pre-allocate!!!
for ii = 1:numel(testData)
Result(:, ii) = sscanf(testData{ii}, '%g;', width);
end
Another approach, which consumes more memory - as long as it fits in the processor cache, this could be faster than the loop:
dummy = sscanf(testData{1}, '%g;', [1, Inf]); % To get the width
width = numel(dummy);
dataStr = sprintf('%s;', testData{:});
Result = sscanf(dataStr, '%g;', [width, Inf]);
It looks like you need to transpose(Result) at the end.
The simple FOR-loop follows the important KISS programming paradigm:
Keep it simple stupid!
When the time required for programming and debugging exceeds the runtime, there is no better concept.

3 comentarios

shaz
shaz el 15 de Nov. de 2012
Editada: shaz el 15 de Nov. de 2012
great.....it works.....thanks
i have a matrix of size(1000000*25) but getting some memory issues
for eg: if testData(:) = {'12; 123; 45; 45; 0.1;12; 123; 45; 45; 0.1;12; 123; 45; 45; 0.1'};
how to handle this situation
Jan
Jan el 15 de Nov. de 2012
This matrix requires 1e6*25*8 bytes of free contiguos RAM, otherwise swapping to disk will destroy any performance. This is only 200 MB and should be available on modern computers. Please explain exactly, which line of code creates which kind of "some memory issues".
shaz
shaz el 15 de Nov. de 2012
preallocation of memory is throwing out of memory
Result = zeros(width, numel(testData)); % Pre-allocate!!!

Iniciar sesión para comentar.

Más respuestas (1)

Azzi Abdelmalek
Azzi Abdelmalek el 15 de Nov. de 2012

0 votos

b=cell2mat(a)

7 comentarios

a={'12' '123' '4';'5' '45' '0.1'}
b=cellfun(@str2double,a)
Azzi Abdelmalek
Azzi Abdelmalek el 15 de Nov. de 2012
Editada: Azzi Abdelmalek el 15 de Nov. de 2012
what is the problem with
b=cellfun(@str2double,a)
I gave just an example, it should work with your case
Azzi Abdelmalek
Azzi Abdelmalek el 15 de Nov. de 2012
please give a sample of your data
Azzi Abdelmalek
Azzi Abdelmalek el 15 de Nov. de 2012
Editada: Azzi Abdelmalek el 15 de Nov. de 2012
If you mean
a={'12 123 4';'5 45 0.1'}
b=cell2mat(cellfun(@str2num,a,'un',0))
Azzi Abdelmalek
Azzi Abdelmalek el 15 de Nov. de 2012
Editada: Azzi Abdelmalek el 15 de Nov. de 2012
I said provide a small example, a=?
a={'12 123 4';'5 45 0.1'}
or
a={'12' '123' '4';'5' '45' '0.1'}
or what?
Azzi Abdelmalek
Azzi Abdelmalek el 15 de Nov. de 2012
Editada: Azzi Abdelmalek el 15 de Nov. de 2012
b=cell2mat(cellfun(@(x) cellfun(@str2double, regexp(x,';','split')),a,'un',0))
Jan
Jan el 15 de Nov. de 2012
@Azzi: str2double accepts cell strings as input. Therefore I'd omit at least the innermost cellfun. cell2mat contains an expensive loop. Therefore it is most likely naticably faster to create this loop manually, because this will reduce the amount of temporarily used memory.

Iniciar sesión para comentar.

Categorías

Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.

Productos

Etiquetas

Preguntada:

el 15 de Nov. de 2012

Community Treasure Hunt

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

Start Hunting!

Translated by