Keep positive real values from a cell inside a matrix

2 次查看(过去 30 天)
Hi,
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).Capture.JPG
Thank you.
  2 个评论
Guillaume
Guillaume 2019-7-12
So, you want to plot points at coordinate X, Y, Z with X and Y from 1 to 21 and 1 to 13, and Z is what? One of the 25 or 26 number for that X and Y? All the 25/26 points (so you'd have columns of point at each X and Y?
Shouldn't the first row of that cell array be made of 26x1 vectors? Just like all the other rows. In which case, the data would be better stored as a 21x13x26 3D array, which would be faster to manipulate, use less memory and a lot simpler to use.
Ilias Minas
Ilias Minas 2019-7-12
Hi Guillauame,
Thank you for your comment.
X and Y will be from 1 to 21 and 1 to 13 respectively. In Z axis i want to have the positive real values for each case.
So want to create a 3d scatter with the positive real values of every cell in Z axis.
For example if in cell (3,2) have 2 positive real values, in the scatter will appear as a two points in Z axis at X=3 and Y=2 point.
I want to scatter all the positive real values of every cell/
Thank you again.

请先登录,再进行评论。

采纳的回答

Guillaume
Guillaume 2019-7-12
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)));

更多回答(1 个)

KSSV
KSSV 2019-7-11
Let A be your cell.
Convert cells into matrix using:
A = cell2mat(A) ;
Pick the real parts of A using:
A = real(A) ;
Pick the posititve reals using:
A(A<0) = NaN ;
surf(A)
  3 个评论
Ilias Minas
Ilias Minas 2019-7-12
I tried this but it gives me only the last value of the for loop.
Can i do it for all the cells separetely?

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by