indices and values

1 次查看(过去 30 天)
huda nawaf
huda nawaf 2011-11-22
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 个评论
huda nawaf
huda nawaf 2011-11-22
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

请先登录,再进行评论。

回答(3 个)

Walter Roberson
Walter Roberson 2011-11-22
textscan(fid, '%*c%f%*c%f%*c%f')
  6 个评论
Andrei Bobrov
Andrei Bobrov 2011-11-23
a = accumarray([w,w1],w2,[],[],[],true)
Walter Roberson
Walter Roberson 2011-11-23
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.

请先登录,再进行评论。


Fangjun Jiang
Fangjun Jiang 2011-11-22
[I,J,V]=textread('test.txt','(%f,%f) %f');
A=zeros(2,3);
A(sub2ind(size(A),I,J))=V

Andrei Bobrov
Andrei Bobrov 2011-11-23
[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 个评论
huda nawaf
huda nawaf 2011-11-23
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 2011-11-23
I got this error when use cellfun
c = cellfun(@(x)double(x),c,'un',0);
??? Too many inputs.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Structures 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by