Minimum value of select cell elements?

1 visualización (últimos 30 días)
Harvey Rael
Harvey Rael el 18 de Jul. de 2018
Respondida: Harvey Rael el 19 de Jul. de 2018
I have a cell array that is 20x50. And inside each cell, there is a 200x1 column vector. Of interest to me is the 200th element in each cell. I want to find the closest to 0 value of all the 200th elements of the entire cell array, and then return the row and column number it occurs in. I am not overly familiar with cell array manipulation in such a way and I don't know if it is possible. So far I have tried:
fpa_lam = 20x50 cell;
for b = 1:size(fpa_lam);
C(b) = fpa_lam{b}(200);
end
in an attempt to get all the 200th elements of each cell into a matrix, where I can then easily manipulate it how I like. No doubt I was a fool to think it would be that easy, can anyone point me where I am going wrong?
Edit: I have realised cellfun is the way to go - so my new code looks like :
b=200;
Final_Angles = cellfun(@(x) x(b), fpa_lam);
Though, getting the "index exceeds array bounds" error. It works in all the notation in the documentation, I don't understand what I am doing wrong.
Edit edit:
Not all cells are the same size. Some are empty. Don't know if that changes things. Its more important that I get the values out into a matrix, for observation reasons rather than just have the answer. I'd like for there to be empty values in the matrix where the cells are empty.
  5 comentarios
Harvey Rael
Harvey Rael el 18 de Jul. de 2018
Yeah, sorry, it was late and I didn't make that clear. Some of them are empty. Returning a NaN when seeing an empty cell would be great, I'm still not sure what I'm doing wrong with cellfun(@x x(200), cellarray) to even get some values, just continually returning error messages
Stephen23
Stephen23 el 18 de Jul. de 2018
"Some of them are empty."
"...I'm still not sure what I'm doing wrong"
Trying to access the 200th element of an empty array is always going to throw an error. You need to use a mask to ignore the empty cells.

Iniciar sesión para comentar.

Respuesta aceptada

Harvey Rael
Harvey Rael el 19 de Jul. de 2018
I found a way to extract the values into a matrix, rather nicely for myself for those wondering. Appreciate all efforts that went into this all the same. Once the elements are in a matrix, manipulations is easy.
for b = 1:size(fpa_lam)
for c= 1:length(fpa_lam)
if isempty(fpa_lam{b,c});
fpaVec(b,c) = NaN;
else fpaVec(b,c) = fpa_lam{b,c}(200);
end
end
end
end

Más respuestas (2)

Image Analyst
Image Analyst el 18 de Jul. de 2018
Here's a way that takes only 0.001 seconds on my machine:
tic
lastValues = inf * ones(20*50, 1);
for index = 1 : 20*50
cellContents = fpa_lam{index};
lastValues(index) = abs(cellContents(end));
end
% Find the value closest to zero, and the cell where it occurs
[lowestLastValue, linearIndex] = min(lastValues)
[row, col] = ind2sub([20, 50], linearIndex)
toc
Maybe not as compact as cellfun(), but a lot more readable and intuitive.
  1 comentario
Harvey Rael
Harvey Rael el 18 de Jul. de 2018
Hi, Thanks for answering! I'm not quite sure why you multiply inf into the ones, nor why you have the ones? Care to walk me through what you're doing?

Iniciar sesión para comentar.


Carlos Felipe Rengifo
Carlos Felipe Rengifo el 18 de Jul. de 2018
Hi, I propose you the following solution:
% The 20x50 cell array is transformed into a 20x(10000) matrix.
elems = cell2mat(fpa_lam);
% "elem200" is a matrix with 20 rows and 50 columns. "elem200(i,j)" is
% equal to fpa_lam{i,j}(200)
elem200 = elems(:,200:200:end);
% Location of the minimal element of the matrix elem200. Here it is
% suppossed that all the elements of "fpa_lam" are non-negatives.
[lowest_by_col,rows] = min(elem200);
[~,colmin] = min(lowest_by_col);
rowmin = rows(colmin);
Please, let me know if this solution satisfies your requirements.
  2 comentarios
Harvey Rael
Harvey Rael el 18 de Jul. de 2018
Hi, Thanks for your answer! I was following until the "elem200 = elems(:,200:200:end). Care to walk me through what is going on there?
Carlos Felipe Rengifo
Carlos Felipe Rengifo el 18 de Jul. de 2018
Editada: Carlos Felipe Rengifo el 18 de Jul. de 2018
Hi, the columns 1 to 200 of the first row of "elems" are the components of the cell fpa_lam(1,1). The columns 201 to 400 of "elems" are the components of the cell fpa_lam(1,2), and so on. For the second row, we have the same pattern: the columns 1 to 200 of the second row of "elems" are the components of the cell fpa_lam(2,1). In this way, the columns 200,400,600, ... of "elems" contain the last element of each cell.
"elems200" is then a matrix with 20 rows and 50 columns containing the element 200 of each array.
If you need further explanations, please do not hesitate to ask again.

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Productos


Versión

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by