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

 채택된 답변

Patrik Ek
Patrik Ek 2014년 2월 4일
편집: Patrik Ek 2014년 2월 4일

1 개 추천

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

Saheed Adio
Saheed Adio 2014년 2월 4일
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 2014년 2월 4일
편집: Patrik Ek 2014년 2월 4일
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.
Patrik Ek
Patrik Ek 2014년 2월 4일
Ok I have edited my answer. I think this is what you want.
Saheed Adio
Saheed Adio 2014년 2월 4일
편집: Saheed Adio 2014년 2월 4일
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.
Patrik Ek
Patrik Ek 2014년 2월 5일
편집: Patrik Ek 2014년 2월 5일
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 2014년 2월 5일
Thanks on this Really appreciate!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2014년 2월 4일

댓글:

2014년 2월 5일

Community Treasure Hunt

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

Start Hunting!

Translated by