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 个评论
采纳的回答
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 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!