1
votes

This is my current matlab code:

a = load('m1.txt');
b = load('m2.txt');
c = a*b;
fid = fopen('Matrix.txt','wt');
for ii = 1:size(c,1)
fprintf(fid,'%g\t',c(ii,:));
fprintf(fid,'\n');
end
fclose(fid)

Basically read in two files and multiply the result to get the multiplied matrix, and write it to a file.

I'm suppose to find out if there is a cache friendly way to do this. But I think matrix somewhat efficient in this area opposed to other programming languages sometimes. Any hints or sample code?

2
You could also use dlmwrite('Matrix1.txt',c,'delimiter', '\t') and skip the for loop.AGS
Just to clarify the answers that have already been given: Probably the slow part of your code is not the matrix multiplication but actually writing out the result.Dennis Jaheruddin

2 Answers

2
votes

Matlab matrix multiplication is really very efficient. I do not think that you can do better than what is already there.

2
votes

You can use the save command to simplify the write to disk loop.

save Matrix.txt c -ascii

This will write to disk the variable 'c' in ascii format.