For loop to calculate different median in a table
Mostrar comentarios más antiguos
Hello,
I'm trying to write a function where I fill a table doing a median but with different values of the same cell variable. I would like to somehow automatize these so when I change my table size could make the calculations.
Right now I have a 5x5 table were I wrote all the calculation by hand:
function [PLUT]=MergeVertex(PLUT,Vp)
PLUT(1,1)=median([Vp{1,1}(1,1)]);
PLUT(2,1)=median([Vp{1,1}(2,1),Vp{2,1}(1,1)]);
PLUT(3,1)=median([Vp{3,1}(1,1),Vp{2,1}(2,1)]);
PLUT(4,1)=median([Vp{4,1}(1,1),Vp{3,1}(2,1)]);
PLUT(5,1)=median([Vp{4,1}(2,1)]);
PLUT(1,2)=median([Vp{1,1}(3,1),Vp{1,2}(1,1)]);
PLUT(2,2)=median([Vp{1,1}(4,1),Vp{2,1}(2,1),Vp{1,2}(3,1),Vp{2,2}(1,1)]);
PLUT(3,2)=median([Vp{2,1}(4,1),Vp{3,1}(3,1),Vp{2,2}(2,1),Vp{3,2}(1,1)]);
PLUT(4,2)=median([Vp{3,1}(4,1),Vp{4,1}(2,1),Vp{3,2}(3,1),Vp{4,2}(1,1)]);
PLUT(5,2)=median([Vp{4,1}(4,1),Vp{4,2}(2,1)]);
PLUT(1,3)=median([Vp{1,2}(3,1),Vp{1,3}(1,1)]);
PLUT(2,3)=median([Vp{1,2}(4,1),Vp{2,2}(3,1),Vp{1,3}(3,1),Vp{2,3}(1,1)]);
PLUT(3,3)=median([Vp{2,2}(4,1),Vp{3,2}(2,1),Vp{2,3}(2,1),Vp{3,3}(1,1)]);
PLUT(4,3)=median([Vp{3,2}(4,1),Vp{4,2}(2,1),Vp{3,3}(2,1),Vp{4,3}(1,1)]);
PLUT(5,3)=median([Vp{4,2}(4,1),Vp{4,3}(2,1)]);
PLUT(1,4)=median([Vp{1,3}(3,1),Vp{1,4}(1,1)]);
PLUT(2,4)=median([Vp{1,3}(4,1),Vp{2,4}(3,1),Vp{1,4}(3,1),Vp{2,4}(1,1)]);
PLUT(3,4)=median([Vp{2,3}(4,1),Vp{3,4}(3,1),Vp{2,4}(3,1),Vp{3,4}(1,1)]);
PLUT(4,4)=median([Vp{3,3}(4,1),Vp{4,4}(3,1),Vp{3,4}(3,1),Vp{4,4}(1,1)]);
PLUT(5,4)=median([Vp{4,3}(4,1),Vp{4,4}(2,1)]);
PLUT(1,5)=median([Vp{1,4}(3,1)]);
PLUT(2,5)=median([Vp{1,4}(4,1),Vp{2,4}(3,1)]);
PLUT(3,5)=median([Vp{2,4}(4,1),Vp{3,4}(3,1)]);
PLUT(4,5)=median([Vp{3,4}(4,1),Vp{4,4}(3,1)]);
PLUT(5,5)=median([Vp{4,4}(4,1)]);
end
I know that for that, I should do something like this, but I dont see how can I do those medians above during the loop.
Ri=5;
Rj=5;
for ii 1:length(Ri)
for jj 1:length(Rj)
PLUT(jj,ii)=median([Vp{1,1}(1,1)]);
end
end
5 comentarios
Image Analyst
el 20 de Oct. de 2022
You forgot to attach your data. It would be easier for people to help you if you attach PLOT and Vp in a .mat file.
If you have any more questions, then attach your data and code to read it in with the paperclip icon after you read this:
Mikel
el 20 de Oct. de 2022
dpb
el 20 de Oct. de 2022
You also forgot to tell us the rule(s) by which you created the above output array from the input so we would have at least some idea about how to go about generalizing it to another case. As is, it's a "discover the pattern" puzzle in the back pocket airline magazine to try to solve first. "Help us help you..."
One additional comment on the code ... In
function [PLUT]=MergeVertex(PLUT,Vp)
there's no purpose in having the output variable also in the input argument list -- you don't ever use it in creating a new output; remove it.
Mikel
el 20 de Oct. de 2022
ag
el 31 de Ag. de 2023
Hi, can you please provide a mathematical representation of how you would like to choose and update the cells.
Respuestas (0)
Categorías
Más información sobre Tables 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!
