How can I find a specific value of member in cell array
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
saif sabeeh
il 24 Dic 2018
Commentato: Guillaume
il 24 Dic 2018
hello,
I have a cell array (100 * 50 cell), each cell contain several variables, like (Ptx in subcarrier(vector 100 valuse) , Position of subchannels(vector 10 valuse) , interfernce(vector 10 valuse) , SubFrame).
I need to find all cells that have SubFrame equal 1.
VH = cell(100,50);
index = find([VH{:,:}.SubFrame] == 1); % but it doesnt work??
What can i do? THanks in advance.
4 Commenti
Guillaume
il 24 Dic 2018
Modificato: Guillaume
il 24 Dic 2018
So, your cells do not contain structures (at least some of them don't) and Matt's and my assumption were wrong. The problem is we still have no idea what is in each cell of the cell array.
Remember that we have absolutely no idea what you're starting with. We don't know what the channels mean. Matlab does not a channel data type. We have absolutely no idea what resource blocks are.
You either need to show some code as requested, or better attach a simple mat file containing that VH cell array.
Risposta accettata
Image Analyst
il 24 Dic 2018
Modificato: Image Analyst
il 24 Dic 2018
A cell cannot contain more than one variable. It can contain only 1. It cannot contain 3 vectors and a scalar unless they parts of a single variable like a structure or table variable. So you could have, say, a structure str, and str had 3 fields, each of which is a 100 or 10 element vector, and one field that is a scalar called SubFrame. Not sure if there is a cryptic way of getting the SubFrame values using cellfun but check the other answers. Below is a brute force way of getting SubFrame
[rows, columns] = size(VH)
allSubFrames = zeros(rows, columns); % Preallocate our output array.
for col = 1 : columns
for row = 1 : rows
thisCellsContents = VH{row, col}; % Extract structure
allSubFrames(row, col) = thisCellsContents.SubFrame; % Extract SubFrame from the structure
end
end
Again, the above assumes your cell contains a structure, not a table.
To find all cells that contain the same SubFrame, you can use unique:
uniqueSubFrames = unique(allSumFrames(:))
Then you can use a loop to check each one number to find out what cell(s) that number appears in.
for k = 1 : length(uniqueSubFrames)
thisNumber = uniqueSubFrames(k);
[sfRows, sfColumns] = find(allSumFrames == thisNumber);
% Now do something with that information....not sure what....
end
4 Commenti
Image Analyst
il 24 Dic 2018
Yes you could do the loop that way, but with the 2-D loop I used, the answers are in the same row and column as the input instead of in a linear array, and that may be helpful depending on what you want to do with the information.
Well maybe not cryptic to you but honestly even to me, and even more so to beginners, a simple for loop is often easier to understand and more intuitive than functions like cellfun(), arrayfun(), blockproc(), accumarray(), im2col() and other types of functions that are more compact. So being easier to understand and maintain is often more important than compactness or efficiency (run time).
Guillaume
il 24 Dic 2018
With my loop, the answer is in a 2d array if the input is 2d, in a 3d array if the input is 3d, etc. (same as with cellfun)
Più risposte (2)
Guillaume
il 24 Dic 2018
It's really not clear from your description but it sounds like each cell of the cell array contains a scalar structure. If that's not the case, then you need to tell us exactly what is in the cells. It's certainly not variables. If that is the case, then:
- Why aren't using a 2D structure instead of a cell array of scalar structures? It would make everything simpler
- The way to do it with your current storage is:
find(cellfun(@(s) s.SubFrame == 1, VH))
- Do you actually need the find. More often than not, the logical array that you pass to find is enough. e.g. if you want to keep only those elements of VH where the subframe is 1. then:
filteredVH = VH(cellfun(@(s) s.SubFrame == 1, VH)); %no need for find
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!