fprintf replace values from xlsread table

1 view (last 30 days)
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 Comments
Anna Cole
Anna Cole on 12 Jun 2018
eventually the column will contain 189604800 zeros but I'm just testing it now
Walter Roberson
Walter Roberson on 12 Jun 2018
How would position 101927062 be converted into an index into b ?

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 12 Jun 2018
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);

More Answers (0)

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!