finding max for each 3 columns

Hi All,
I have a dataset something similar to following
Name Location Nickname saleCost
rest1 east Reast1 [5, 7, 9 , 2, 5, 1, 9, 11, 1]
rest4 south Rsouth4 [12, 9, 2, 3, 5, 10, 11, 4, 4]
rest3 east Reast3 [20, 2, 2, 3, 20, 30, 1, 3, 7]
rest9 west Rwest9 [1, 9, 3, 33, 22, 10, 6, 6, 8]
Note that saleCost is a dataset as well and as number of columns which are multiple of 3.
I want to find max for every 3 columns so my resultant dataset is as follow
Name Location Nickname saleCostMax
rest1 east Reast1 [9, 9, 9, 5, 9, 11, 11, 11, 1]
rest4 south Rsouth4 [12, 9, 5, 10, 11, 11, 11, 4, 4]
rest3 east Reast3 [20, 3, 20, 30, 30, 30, 7, 7, 7]
rest9 west Rwest9 [9, 33, 33, 33, 22, 10, 8, 8, 8]
ANy other way so that I do not have to use for loop? WHat are my options?
Thanks

2 comentarios

Geoff Hayes
Geoff Hayes el 4 de Nov. de 2015
Neesha - is your data set a cell array or something else? Why don't you want to use a for loop?
Neesha
Neesha el 4 de Nov. de 2015
isn't for loop time consuming and not the best approach? I will have 100s of lines

Iniciar sesión para comentar.

 Respuesta aceptada

Image Analyst
Image Analyst el 4 de Nov. de 2015
If you have the Image Processing Toolbox, you can go down row by row and call imdilate() to get a moving max:
thisRow = [5, 7, 9 , 2, 5, 1, 9, 11, 1]
newRow = imdilate(thisRow, [1,1,1])
% Crop off the first element and tack on the last element
newRow = [newRow(2:end), thisRow(end)]
In the command window:
thisRow =
5 7 9 2 5 1 9 11 1
newRow =
7 9 9 9 5 9 11 11 11
newRow =
9 9 9 5 9 11 11 11 1

4 comentarios

Neesha
Neesha el 4 de Nov. de 2015
seems it worked in this example for me. :) if i have to take max of every 6 instead of 3, do i have keep changing newRow? How?
Image Analyst
Image Analyst el 5 de Nov. de 2015
The kernel in imdilate() defines what elements in a sliding window to look at. If you wanted to look at 6 use [1,1,1,1,1,1]. Use a zero if you want to ignore an element.
Neesha
Neesha el 5 de Nov. de 2015
ok makes sense. I tried and this is what i get. It is not correct output
thisRow = [5, 7, 9 , 2, 5, 1, 9, 11, 1]
newRow = imdilate(thisRow, [1,1,1,1,1,1])
% Crop off the first element and tack on the last element
newRow = [newRow(2:end), thisRow(end)]
thisRow =
5 7 9 2 5 1 9 11 1
newRow =
9 9 9 9 9 11 11 11 11
newRow =
9 9 9 9 11 11 11 11 1
Image Analyst
Image Analyst el 5 de Nov. de 2015
What would you want? A sliding window of 6 only fits completely in there in 4 spots. Do you want 4 output numbers, or do you want the window to slide off on both ends?

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Startup and Shutdown en Centro de ayuda y File Exchange.

Productos

Etiquetas

Aún no se han introducido etiquetas.

Preguntada:

el 4 de Nov. de 2015

Comentada:

el 5 de Nov. de 2015

Community Treasure Hunt

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

Start Hunting!

Translated by