fprintf replace values from xlsread table

1 次查看(过去 30 天)
I have a column of 100 zeroes and in certain positions in the column I want them to be replaced with a value. The position and replacement values are held inside the trial.xlsx file. What could I use to do this. Previously I was using b(positionvalue)=replacementvalue but now there are too many position values to just type them in.
b=zeros(100,1);
a = xlsread('trial.xlsx','Sheet1','A2:B1473');
c = 9 * ones(length(b),1);
c(b==0) = 1;
d=[c,b];
fileID = fopen('xlsreadtrial.txt','w');
fprintf(fileID,'%.*f\n',d');
fclose(fileID);
  5 个评论
Anna Cole
Anna Cole 2018-6-12
eventually the column will contain 189604800 zeros but I'm just testing it now
Walter Roberson
Walter Roberson 2018-6-12
How would position 101927062 be converted into an index into b ?

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2018-6-12
Under the assumption that the entries in column 1 are positive integers representing position, and that the number of entries might be significantly less than the maximum index, then:
b = sparse(1, a(:,1), a(:,2));
This also has the property that duplicate entries will be totaled. If you wanted them averaged instead, or want min or max of the duplicate entries, then there are relatively simple ways using accumarray().
In the case where the number of entries covers most of the maximum values, and assuming that there are no duplicate indices, then
b(a(:,1)) = a(:,2);

更多回答(0 个)

类别

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

产品


版本

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by