Minimum value of select cell elements?
조회 수: 1 (최근 30일)
이전 댓글 표시
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
Stephen23
2018년 7월 18일
"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.
채택된 답변
추가 답변 (2개)
Image Analyst
2018년 7월 18일
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
2018년 7월 18일
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
Carlos Felipe Rengifo
2018년 7월 18일
편집: Carlos Felipe Rengifo
2018년 7월 18일
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.
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!