A {1x4}
Such that
A = {A{1,1},A{1,2},A{1,3},A{1,4}}
A{1,1}={}
A{1,2}={0.5 0.2
1 2
3 4
NaN NaN
5 6
7 8
9 10
NaN NaN}
A{1,3}={0.5 0.8
5 6
7 8
9 10
NaN NaN
11 12
13 14
15 16
17 18
NaN NaN}
A {1,4} = {0.7 0.9
19 20
NaN NaN
25 26
27 28
29 30
31 32
NaN NaN}
I want to separate data when there is NaN NaN in the matrix, want to store values before first NaN NaN in one cell array and after NaN NaN in another cell array.
B1 = {A{1, 2}(1:3,:))}
B2 = {A{1, 3}(1:4,:))}
B3 = {A{1, 4}(1:2,:))}
C1 = {A{1, 2}(5:7,:)}
C2 = {A{1, 3}(6:9,:)}
C3 = {A{1, 4}(4:7,:)}
B = {{B1},{B2},{B}}
C = {{C1},{C2},{C3}}

1 comentario

Luna
Luna el 10 de Mzo. de 2020
Editada: Luna el 10 de Mzo. de 2020
why are you using cell arrays inside cell arrays? What makes you understand which part belongs to B and which belongs to C?

Iniciar sesión para comentar.

 Respuesta aceptada

Ameer Hamza
Ameer Hamza el 10 de Mzo. de 2020
Are you looking for something like this code? Note that I have made some modifications to the datatypes and removed A{1,1} = {}, since it makes the code simpler. This code will work as long as there are two nan rows in the matrix.
A{1,1}=[0.5 0.2
1 2
3 4
NaN NaN
5 6
7 8
9 10
NaN NaN];
A{1,2}=[0.5 0.8
5 6
7 8
9 10
NaN NaN
11 12
13 14
15 16
17 18
NaN NaN];
A{1,3} = [0.7 0.9
19 20
NaN NaN
25 26
27 28
29 30
31 32
NaN NaN];
nan_rows = cellfun(@(x) find(isnan(x), 2), A, 'UniformOutput', 0);
B = cellfun(@(x, y) x(1:y(1)-1, :), A, nan_rows, 'UniformOutput', 0);
C = cellfun(@(x, y) x(y(1)+1:y(2)-1, :), A, nan_rows, 'UniformOutput', 0);

13 comentarios

Akshay Sahu
Akshay Sahu el 12 de Mzo. de 2020
It's not working.
Ameer Hamza
Ameer Hamza el 12 de Mzo. de 2020
Is the code not running?
Akshay Sahu
Akshay Sahu el 13 de Mzo. de 2020
Yes it's showing that isnan is undefined
Ameer Hamza
Ameer Hamza el 13 de Mzo. de 2020
Which MATLAB release are you using. isnan() have been defined since R2006a, and it is a part of MATLAB language itself, you don't need some special toolbox. Maybe there is some issue with MATLAB installation. Try
which isnan
What is the output?
Akshay Sahu
Akshay Sahu el 13 de Mzo. de 2020
Akshay Sahu
Akshay Sahu el 13 de Mzo. de 2020
I'm using Matlab2014
Ameer Hamza
Ameer Hamza el 13 de Mzo. de 2020
I am not sure of the problem, find function is also defined since R2006a. Try
which find
Guillaume
Guillaume el 13 de Mzo. de 2020
looks like a very corrupt matlab installation. Both find and isnan have been built-in functions since forever.
Akshay Sahu
Akshay Sahu el 13 de Mzo. de 2020
I think problem is cell array inside cell array??
because both funtions are working properly on matrix
so now any one has the solution that how can I convert cell array inside a cell array to matrix form
Ameer Hamza
Ameer Hamza el 13 de Mzo. de 2020
To avoid nested cell arrays, define A like I have given in my answer. Or
A = {[0.5 0.2; ....], [0.5 0.8; ...], [...]}
Akshay Sahu
Akshay Sahu el 13 de Mzo. de 2020
As you were saying that, this code will work as long as there are two nan rows in the matrix but what if we don't know number of nan rows then is it possible to save them in different cell array using loops?
Ameer Hamza
Ameer Hamza el 14 de Mzo. de 2020
Try this. B is a cell array of cell arrays. See the result with indexing like this: B{1}{1}, B{1}{2}, B{2}{1}, ....
A{1,1}=[0.5 0.2
1 2
3 4
NaN NaN
5 6
7 8
9 10
NaN NaN
0.5 0.2
1 2
3 4
NaN NaN];
A{1,2}=[0.5 0.8
5 6
7 8
9 10
NaN NaN
11 12
13 14
15 16
17 18
NaN NaN
0.5 0.8
5 6
7 8
9 10
NaN NaN];
A{1,3} = [0.7 0.9
19 20
NaN NaN
25 26
27 28
29 30
31 32
NaN NaN
0.7 0.9
19 20
NaN NaN];
total_nan_rows = sum(isnan(A{1}(:,1)));
nan_rows = cellfun(@(x) find(isnan(x), total_nan_rows), A, 'UniformOutput', 0);
for i=1:total_nan_rows
if i==1
B{i} = cellfun(@(x, y) x(1:y(i)-1, :), A, nan_rows, 'UniformOutput', 0);
elseif i==total_nan_rows
B{i} = cellfun(@(x, y) x(y(i-1)+1:end-1, :), A, nan_rows, 'UniformOutput', 0);
else
B{i} = cellfun(@(x, y) x(y(i-1)+1:y(i)-1, :), A, nan_rows, 'UniformOutput', 0);
end
end
Akshay Sahu
Akshay Sahu el 14 de Mzo. de 2020
Yeah thanks man Ameer Hamza it's working.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Get Started with MATLAB en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 10 de Mzo. de 2020

Comentada:

el 14 de Mzo. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by