indices and values

HI,
If I have this txt file(indices and values),and need read it into array without using for loop. Is there command do that?
(1,2) 3
(2,3) 4
thanks

1 Comment

huda nawaf
huda nawaf on 22 Nov 2011
sorry ,I were not clear.
I mean a command place each element in it's index
where the above file be :
0 3 0
0 0 4

Sign in to comment.

Answers (3)

textscan(fid, '%*c%f%*c%f%*c%f')

6 Comments

c = textscan(fid, '%*c%f%*c%f%*c%f');
sparse(c{1},c{2},c{3})
c = textscan(fid, '%*c%f%*c%f%*c%f');
accumarray([c{1},c{2}], c{3})
huda nawaf
huda nawaf on 23 Nov 2011
thanks ,
it is perfect command. but unfortunately,the same problem.
if there is no way,then I have to add RAM to my computer.
Look:
fid = fopen('d:\matlab7\work\flixsterdata\finalflix0.txt','r');
>> c = textscan(fid, '(%d,%d)\t%d');
>> c1=double(c{1});
>> c2=double(c{2});
>> c3=double(c{3});
>> w=double(c1); w1=double(c2); w2=double(c3);
>> a=accumarray([w,w1],w2);
??? Error using ==> accumarray
Out of memory. Type HELP MEMORY for your options.
How much RAM and swap space are you configured for now?
You are using the MATLAB Student Version, aren't you? Those are officially only the 32 bit versions, so if you cannot configure enough RAM and swap space in your existing system, then you will not be able to read larger files by getting more RAM.
Have you considered splitting your files in to even smaller pieces?
I have completely lost track of what you intend to do with the arrays once you have read them in?
a = accumarray([w,w1],w2,[],[],[],true)
Interesting, Andrei; I had quite forgotten that flag.
I am, however, not convinced at the moment that the array really is sparse; Huda earlier (in a different Question on this same topic) made reference to the array indices being sequential.

Sign in to comment.

Fangjun Jiang
Fangjun Jiang on 22 Nov 2011
[I,J,V]=textread('test.txt','(%f,%f) %f');
A=zeros(2,3);
A(sub2ind(size(A),I,J))=V
Andrei Bobrov
Andrei Bobrov on 23 Nov 2011
[EDIT]
fid = fopen('nameyourfile.txt','r');
c = textscan(fid, '(%f,%f)%f','collectoutput',true);
fclose(fid)
a = accumarray(c{1}(:,1:2),c{1}(:,3),[],[],[],true)
[ADD] 22:32MSD
fid = fopen('nameyourfile.txt','r');
c1 = textscan(fid, '(%f,%f)%f');
fclose(fid);
a = sparse(c1{:});

2 Comments

huda nawaf
huda nawaf on 23 Nov 2011
I got this error when ran it:
??? Function 'accumarray' is not defined for values of class 'int32'.
Error in ==> accumarray at 73
[varargout{1:nargout}] = builtin('accumarray', varargin{:});
huda nawaf
huda nawaf on 23 Nov 2011
I got this error when use cellfun
c = cellfun(@(x)double(x),c,'un',0);
??? Too many inputs.

Sign in to comment.

Categories

Tags

Asked:

on 22 Nov 2011

Community Treasure Hunt

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

Start Hunting!