Iteration across numerous columns
조회 수: 2 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
채택된 답변
Patrik Ek
2014년 2월 4일
편집: Patrik Ek
2014년 2월 4일
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
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.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Graphics Object Properties에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!