# Composición de matrices MaxMin

I'm working on fuzzy classifications. Because I'm not a real software developer... just realized that I'm not able to implement, in `C` language, the max-min composition on matrices.

I'll try to be much more clear.

Suppose you've got a square matrix just like this:

``````float matrix[2][2] = {
{ 1.0, 0.4 },
{ 0.4, 1.0 }
};
``````

The max-min composition of "matrix" by itself, yelds

``````result_matrix[2][2] = {
{ 1.0, 0.4 }
{ 0.4, 1.0 }
};
``````

p.ej

first perform

``````min(1.0, 0.4) = 0.4 /* first row */
min(1.0, 0.4) = 0.4 /* first col */
``````

luego

`max(0.4, 0.4) = 0.4` that's the element at (0, 0) of the new matrix.

p.ej

``````min(1.0, 0.4) = 0.4; /* first row */
min(0.4, 1.0) = 0.4; /* second col */

max(0.4, 0.4) = 0.4;
``````

element at (0, 1)

What I need to implement, in `C` language, is a sort of "rows by cols" check on a matrix.

I really dunno how to do this.

Algunas sugerencias ?

Gracias de antemano.

preguntado el 05 de mayo de 13 a las 18:05

## 4 Respuestas

Just figured out that, to solve this, I should follow an approach similar to the one used for matrices multiplication.

En lugar de

``````resultMatrix[i][j] += firstMatrix[i][k] * firstMatrix[k][j];
``````

necesito algo como esto

``````resultMatrix[i][j] = MAX(MIN(firstMatrix[i][k], firstMatrix[k][j]));
``````

Dónde `MIN(firstMatrix[i][k], firstMatrix[k][j])` is just another array.

Supongo.

contestado el 06 de mayo de 13 a las 16:05

¡Resuelto!

Here the source code for max-min composition.

``````#include <stdio.h>
#include <stdlib.h>

float get_max(float a[], int num_elements);

int main(int argc, char **argv)
{
int i;

int j;

int k;

float firstMatrix[4][4] = {
{ 1, 0.6, 0.3, 0.8 },
{ 0.6, 1, 0.1, 0.4 },
{ 0.3, 0.1, 1, 0.5 },
{ 0.8, 0.4, 0.5, 1 }
};

float resultMatrix[4][4];

float min_array[4];

for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
for (k = 0; k < 4; k++)
{
if (firstMatrix[i][k] <= firstMatrix[k][j])
{ min_array[k] = firstMatrix[i][k]; }

else
{ min_array[k] = firstMatrix[k][j]; }

resultMatrix[i][j] = get_max(min_array, 4);
}

fprintf(stdout, "%.1f ", resultMatrix[i][j]);
}

fprintf(stdout, "\n");
}

return 0;
}

float get_max(float a[], int num_elements)
{
int i;

float max = 0.0;

for (i = 0; i < num_elements; i++)
{
if (a[i] > max)
{ max = a[i]; }
}

return(max);
}
``````

contestado el 06 de mayo de 13 a las 20:05

You really don't need three answers for this question; you should edit esta respuesta with the contents of this answer. - LittleBobbyTables - Au Revoir

Could you be a bit more specific as to what rows/columns you are comparing? If I knew exactly what you were comparing and in what order I could help you out more. This is what I can give you so far. Here is a function for finding the minimum value of an array

``````float min(int n, float *array)
{
int i;
float minval;

minval = *array;

for (i=1; i<n;i++)
{
if (array[i] < minval)
minval = array[i];
}

return minval;
}
``````

To find the minimum value of the first row you would use

``````x = min(2, *matrix);
``````

For other rows

``````x = min(NUMCOLS, *matrix+row*NUMCOLS + column));
``````

And to access the elements of the matrix by rows over columns use two nested for loops

``````for (i = 0; i < 2; i++)
for (j = 0; j < 2; j++)
printf("%f\n", matrix[i][j]);
``````

Echa un vistazo a Row-major_order

contestado el 05 de mayo de 13 a las 23:05

I guess this will help.

The "algorithm" should calculates each new element of a new matrix.

Por cierto...

You have an input matrix such as `firstMatrix`.

Step one: get the first element from the first row of `firstMatrix` and the first element of the first col of `firstMatrix`: `firstMatrix[0][0]` y `firstMatrix[0][0]`

Step two: repeat "Step one" for each row and col of `firstMatrix`:

``````/* row */
temp_array_1[i] = firstMatrix[i][j]
``````

y

``````/* col */
temp_array_2[i] = firstMatrix[j][i]
``````

Paso tres:

``````for (i = 0; i < 4; i++)
{
if (temp_array_1[i] <= temp_array_2[i])
{ min_array[i] = temp_array_1[i]; }

else
{ min_array[i] = temp_array_2[i]; }
}
``````

Step four: get the max value in `min_array[i]`.

Here follows my source code...

``````#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
int i;

int j;

float firstMatrix[4][4] = {
{ 1, 0.6, 0.3, 0.8 },
{ 0.6, 1, 0.1, 0.4 },
{ 0.3, 0.1, 1, 0.5 },
{ 0.8, 0.4, 0.5, 1 }
};

float max = 0.0;

float temp_array_1[4];

float temp_array_2[4];

float min_array[4];

for (i = 0; i < 4; i++)
{
/* row */
temp_array_1[i] = firstMatrix[0][i];

/* col */
temp_array_2[i] = firstMatrix[i][0];
}

for (i = 0; i < 4; i++)
{
if (temp_array_1[i] <= temp_array_2[i])
{ min_array[i] = temp_array_1[i]; }

else
{ min_array[i] = temp_array_2[i]; }

for (i = 0; i < 4; i++)
{
if (min_array[i] > max)
{ max = min_array[i]; }
}
}

fprintf(stdout, "\nMax element: %.1f\n", max);

return 0;
}
``````

The point is that I'm not able to "iterate" this for each element of `firstMatrix`.

The output matrix that comes out, from this pandemonium is this:

``````outputMatrix[4][4] = {
{ 1.0, 0.6, 0.5, 0.8 },
{ 0.6, 1.0, 0.4, 0.6 },
{ 0.5, 0.4, 1.0, 0.5 },
{ 0.8, 0.6, 0.5, 1.0 }
}
``````

contestado el 06 de mayo de 13 a las 12:05

Is this an answer, or a response to Roberto Gomez? If the latter, you should edit your question with this information, not post it as a new answer. - LittleBobbyTables - Au Revoir

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.