For loop to calculate different median in a table

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

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
Mikel el 20 de Oct. de 2022
yeah, sorry! I thought I attached the mat file.
As I said PLUT is a 5x5 table and Vp is a 4x4 cell table with a 4x1 array
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
Mikel el 20 de Oct. de 2022
Hello, thanks for helping me.
I'm working in a look up table script where the PLUT table has 25 vertexes. After some calculations I got the Vp cell matrix where every cell represents the estimation of the vertexes of all the regions that has this PLUT table (16). Each region shares with the contigous one some of the vertexes so the function that I try to build, calculates the median for every vertex estimation in the table using the data from Vp.
Sorry if I didn´t explain well, maybe an image could help. These is the table and the calculation that I do is the median among the data in each vertex.
So, in the future if want to create a table of lets say 20x20, I would like to somehow run this function for this new table.
The only reason that I created this functions is because I have a lot of code before, so I want to clean the script only having few lines for the main program. The input PLUT is an older version of the table and the output is the update of the table.
ag
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.

Iniciar sesión para comentar.

Respuestas (0)

Categorías

Productos

Versión

R2021b

Preguntada:

el 20 de Oct. de 2022

Comentada:

ag
el 31 de Ag. de 2023

Community Treasure Hunt

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

Start Hunting!

Translated by