Keep positive real values from a cell inside a matrix
5 views (last 30 days)
I have a matrix 21x13. Inside every cell there are 26 complex values.
I want to keep only the real positive parts of those complex values and plot a 3d graph having in one axis the real positive values, in the other axis the number of matrix rows (1-21)and on the third axis the number of matrix columns(1-13).
Guillaume on 12 Jul 2019
I want to scatter all the positive real values of every cell
In that case, I would convert your cell array to a 3D matrix. As I commented, it's much easier to work with. It's strange that the first row of your cell array has one less element that the others, but we can just pad the shorter cells with NaN.
%input: c, a MxN cell array of P(m,n)x1 complex vectors
maxlength = max(cellfun(@numel, c(:))); %find the length of the largest cell
permutedpadded = cellfun(@(m) permute([m; nan(numel(m) - maxlength)], [3, 2, 1])); %pad all cells so they're the same size. At the same time move the rows to the 3rd dimension
asmatrix = cell2mat(permutedpadded); %and convert into a MxNxmaxP matrix
%now plotting is trivial:
toplot = real(asmatrix) >= 0; %only want the element whose real positive parts
[x, y, ~] = ndgrid(1:size(asmatrix, 1), 1:size(asmatrix, 2), 1:size(asmatrix, 3)); %create array of X, Y coordinates the same size as the matrix
scatter3(x(toplot), y(toplot), real(asmatrix(toplot)));