Plotting in for loop not working. Averaging matrix over ranges.
Mostrar comentarios más antiguos
I thought i had a code that worked but i noticed a tiny error, and ive been banging my head on a brick wall sleeplessly trying to get it to work for days.
Heres my code, i hate it, it doesn't work, if you reckon you can fix it please tell me otherwise just skip this bit and as I've decide to start again.
function TempSalplot(datafile)
datafile=importdata(datafile);
A=datafile.data;
figure;
col = flipud(parula(round(max(datafile.data(:,28)))));
%depth,distance,salinity,temp are in column 28,2,14,11 of datafile respectively;
for d=[1:3:max(datafile.data(:,28))]; %max is around 16
for avd=[1:length(A(:,28))];
if A(avd,28)>d-0.5 & A(avd,28)<d+0.5;
B(avd,1)=A(avd,2);
B(avd,2)=A(avd,14);
B(avd,3)=A(avd,11);
B(all(B==0,2),:)=[];
end
[M,~,z] = unique(B(:,1),'stable');
M(:,2) = accumarray(z,B(:,2),[],@mean);
M(:,3) = accumarray(z,B(:,3),[],@mean);
plot(M(:,2),M(:,3),'o-','color',col(d,:));
hold on;
end
xlabel('Salinity (psu)');
ylabel('Tempurature (degC)');
h=colorbar('ticks',[1:3:round(max(datafile.data(:,28)))]);
h.Label.String=('Depth (m)');
caxis([1,max(d)]);
colormap(col);
end
Essentially i have 4 columns of interest say (a,b,c,d). I would like to average b and c over certain ranges of d. so I would only have as many b,c pairs for a certain d range as there are a values. Then I would like to plot that and redo it for another a range.
data=
0 15 33 0.5
0 15 32 0.8
0 16 32 1.3
0 13 34 1.6
10 13 34 0.6
10 14 35 1.0
10 16 36 1.5
10 12 33 1.9
25 13 34 0.6
25 12 33 0.9
25 12 34 1.6
25 12 36 1.9
What I think I need is a for loop.
1st iteration for 0<d=<1 (d=0.5).
M=
0 15 32.5 0.5
10 13.5 34.5 0.5
25 12.5 33.5 0.5
plot(M(:,2),M(:,3));
then repeat for d=1.5 (1<d=<2) however some of the later iteration for example (10<d=<11) have no measurements for certain values of a so should be shorter than the earlier iterations and therefore plot fewer points.
Any help on this is really appreciated , this has been a nightmare for me, thinking I had it working so many times.
1 comentario
Stephen23
el 13 de Oct. de 2015
[M,~,z] = unique(B(:,1),'stable');
M(:,2) = accumarray(z,B(:,2),[],@mean);
M(:,3) = accumarray(z,B(:,3),[],@mean);
and yet you have not accepted my answer to that question.
Respuesta aceptada
Más respuestas (1)
Eng. Fredius Magige
el 13 de Oct. de 2015
0 votos
Hi You might review few lines, specially for and if, as to be:
for d=1:3:max(datafile.data(:,28)) %max is around 16 for avd=1:length(A(:,28)) if (d<A(avd,28)+0.5 && d>A(avd,28)-0.5)
try it
1 comentario
aaron Harvey
el 13 de Oct. de 2015
Editada: aaron Harvey
el 13 de Oct. de 2015
Categorías
Más información sobre Programming en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
