Minimum value of select cell elements?
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
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
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.
Respuesta aceptada
Más respuestas (2)
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.
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
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.
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!