Minimum value of select cell elements?

1 visualizzazione (ultimi 30 giorni)
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 Commenti
Harvey Rael
Harvey Rael il 18 Lug 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 il 18 Lug 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.

Accedi per commentare.

Risposta accettata

Harvey Rael
Harvey Rael il 19 Lug 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

Più risposte (2)

Image Analyst
Image Analyst il 18 Lug 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 Commento
Harvey Rael
Harvey Rael il 18 Lug 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?

Accedi per commentare.


Carlos Felipe Rengifo
Carlos Felipe Rengifo il 18 Lug 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 Commenti
Harvey Rael
Harvey Rael il 18 Lug 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 il 18 Lug 2018
Modificato: Carlos Felipe Rengifo il 18 Lug 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.

Accedi per commentare.

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Prodotti


Release

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by