Iteration across numerous columns

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

I am not reshaping the data.
See the .csv file attached. There are 12 columns in total (6 in pairs of Viscosity and Temperature).
for ever multiple entries in temperature column, the corresponding viscosity data need to be averaged and both the temperature and mean viscosity need to be returned.
This i need to do for all the pairs.
Sorry for not attaching the file at first, but now attached.
Patrik Ek
Patrik Ek on 4 Feb 2014
Edited: Patrik Ek on 4 Feb 2014
You mean the you need to do this for every column separately? "For all the pairs" is a bit ambigous in this context, you need to be more specific.
Ok I have edited my answer. I think this is what you want.
Thanks Patrik, I also figured a way out but what do u think is the diff. between your code and this. Though this is longer.
for i=2:2:12; %
for j=1:2:12
if j<i;
[u,~,ix] = unique(matrix(:,i));
b = [u,accumarray(ix,matrix(:,j))./accumarray(ix,1)];
display (b)
else
display (b)
end
end
end
Thanks!!!
Edit:
I figure now; the one you posted is faster than this.
One last thing How do I write this into an excel file as separate column?
I used xlswrite('mydata.xls', b, 1, 'A1') after the display code but at the end I realized the previous data were over written,such that it the last iteration that was eventually saved.
Thanks.
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.
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!