Iteration across numerous columns

2 Ansichten (letzte 30 Tage)
Saheed Adio
Saheed Adio am 4 Feb. 2014
Kommentiert: Saheed Adio am 5 Feb. 2014
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

Akzeptierte Antwort

Patrik Ek
Patrik Ek am 4 Feb. 2014
Bearbeitet: Patrik Ek am 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 Kommentare
Patrik Ek
Patrik Ek am 5 Feb. 2014
Bearbeitet: Patrik Ek am 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 am 5 Feb. 2014
Thanks on this Really appreciate!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Graphics Object Properties finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by