Iteration across numerous columns

2 views (last 30 days)
alumina 80 is a matrix of 11143 x 12.
The columns are in pair of (viscosity and temperature) making six pairs. There are multiple entries as well which i need to find the mean and return the answers in two separate columns. I have accomplished this with the code below for a single pair (viscosity and temp).
[u,~,ix] = unique(zz(:,2));
b = [u,accumarray(ix,zz(:,1))./accumarray(ix,1)];
(Note: here zz is a .csv file imported into matlab as a matrix of 11148x2 size).
My next task is to work through column 1&2, 3&4, 5&6, 7&8, 9&10, and 11&12 in that order in a single .csv file imported into MATLAB as matrix of 11143 x 12 size. I also need to return each pair as the code above will do for a single pair of columns.
I tried this code:
[r, c] = size(m);
for t=2:2:c;
for k=1:2:c;
[u,~,ix] = unique(m(:,t));
b = [u,accumarray(ix,m(:,k))./accumarray(ix,1)];
display (b)
end
end
But this didn't work out for me.
Any help please. A sample file has been attached.
Thanks in advance

Accepted Answer

Patrik Ek
Patrik Ek on 4 Feb 2014
Edited: Patrik Ek on 4 Feb 2014
Ok I am still not sure what you meant but I think I have an idea. If you want the mean viscosity for each temperature, for every column pair, I would adjust your loop a bit. Try to remove the outer loop and change index on the inner
[r, c] = size(m);
%for t=2:2:c;
for k=2:2:c;
[u,~,ix] = unique(m(:,k));
b = [u,accumarray(ix,m(:,k-1))./accumarray(ix,1)];
display (b)
end
%end
This should work. If this is what you wanted. You vectorize the calculation for each row, which is why a second for loop is not needed
  6 Comments
Patrik Ek
Patrik Ek on 5 Feb 2014
Edited: Patrik Ek on 5 Feb 2014
The error you did was that you write the first element in 'A1' everytime. There are also numerous reasons to write to file only once (runtime if nothing else).
Also,nan elements are not written to the xls, so what you can do is create a nan matrix that you fill up. Try,
m = [1 3 2 7;1 2 2 3;1 4 2 2;4 5 6 7]';
c = 4;
xlsMtx = nan(size(m));
for k=2:2:c;
[u,~,ix] = unique(m(:,k));
b = [u,accumarray(ix,m(:,k-1))./accumarray(ix,1)];
display (b)
xlsMtx(1:length(b),k-1:k) = b;
end
display(xlsMtx);
xlswrite('test.xls',xlsMtx)
Which writes the result to an excel sheet. And also, the second loop should be removed! It calculate the result for i/2 times which means that with i=12, the same calculation is done 6 times which is a waste. If you are going to create a matrix as in this comment the result of the second loop could even be disastrous, since it will overwrite all the previous results. This loop have nothing to do there and can only cause destruction. You want the elements matrix(:,i) and the previous column, so tell matlab to do just this and remove the loop.
Saheed Adio
Saheed Adio on 5 Feb 2014
Thanks on this Really appreciate!

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!