Working with a three dimensional matrix

I have a matrix with three dimensions, and I need to take two dimensions through the use of cycle FOR.
For example:
Name variable Value
k <39x17x60>
And I need this:
a = 60;
for i = 1:60;
l = k...; %size of l <39x17>
xlswrite...;
end

 Respuesta aceptada

Oleg Komarov
Oleg Komarov el 25 de En. de 2011
The problem is how you construct kunn, in general there are so many nested loops that my suspect is you end up overwriting the data, go in debug mode and see what happens each time you increase the frist loop and so on:
function[F0mean,F0max,F0min,F0max_min,F0std,F0median,relF0VR,relF0SD,...
Emean,Emax,Emin,Emax_min,Estd,Emedian,TEO,Jmean,...
Jmax,Jmin,Jmax_min,Jstd,Jmedian,Smean,Smax,Smin,...
Smax_min,Sstd,Smedian,F1mean,F1max,F1min,F1max_min,F1std,...
F1median,F2mean,F2max,F2min,F2max_min,F2std,F2median]...
= HRA()
[wav,lab]=databaze;
wav = wav';
lab = lab';
L = length(wav);
for j = 2:L+1
brd = process_lab_file(lab{j-1});
label = brd(:,1);
K = length(label);
for i = 1:K
[F0mean,F0max,F0min,F0max_min,F0std,F0median,relF0VR,relF0SD,...
Emean,Emax, Emin,Emax_min, Estd, Emedian, TEO,...
Jmean,Jmax,Jmin,Jmax_min,Jstd,Jmedian,...
Smean,Smax,Smin,Smax_min,Sstd,Smedian,...
F1mean,F1max,F1min,F1max_min,F1std,F1median,...
F2mean,F2max,F2min,F2max_min,F2std,F2median]=extr_part_of_sig(wav{j-1},label(i),lab{j-1});
priznaky =...
[F0mean,F0max,F0min,F0max_min,F0std,F0median,relF0VR,relF0SD,...
Emean,Emax, Emin,Emax_min, Estd, Emedian, TEO,...
Jmean,Jmax,Jmin,Jmax_min,Jstd,Jmedian,...
Smean,Smax,Smin,Smax_min,Sstd,Smedian,...
F1mean,F1max,F1min,F1max_min,F1std,F1median,...
F2mean,F2max,F2min,F2max_min,F2std,F2median];
O = length(priznaky);
for o = 1:O
zap(o,i) = priznaky(o);
end
end
F = length(priznaky);
for i = 1:K
for a = 1:F
kunn(a,j-1,i) = zap(a);
end
end
end
S = length(label);
jaj = zeros(39,17);
for f = 1:S
jaj = kunn(:,:,2);
koj = jaj';
%xlswrite('ZK.xls', koj, label{i}, 'B2');
end

1 comentario

Pepa
Pepa el 25 de En. de 2011
In debug I found error. You have right. Matrix in kunn is same...

Iniciar sesión para comentar.

Más respuestas (1)

Kenneth Eaton
Kenneth Eaton el 24 de En. de 2011
If you're asking how you would index k in your loop to get a 2-D submatrix, you would do the following:
l = k(:,:,i);

14 comentarios

Paulo Silva
Paulo Silva el 24 de En. de 2011
I wasn't fast enough, you posted first :)
% k <39x17x60>
k=randn(39,17,60); %example data
%And I need this:
l=zeros(39,17); %preallocation of l
for i = 1:60
l=k(:,:,i); %size of l <39x17>
%xlswrite...;
end
Pepa
Pepa el 25 de En. de 2011
In your case it works, but in my case it doesn't work, but i don't know why.
S = length(label);
jaj = zeros(39,17);
for f = 1:S
jaj = kunn(:,:,f);
koj = jaj';
%xlswrite('ZK.xls', koj, label{i}, 'B2');
end
Paulo Silva
Paulo Silva el 25 de En. de 2011
please explain exactly what doesn't work in your simple code?
Pepa
Pepa el 25 de En. de 2011
I don't know where is the problem. But it does't work how i expected.
If i changed code:
jaj = kunn(:,:,2); (or diferent number it does't matter and in variable jaj is first matrix (39x17))
Screenshot of variables:
http://img153.imageshack.us/i/89568725.gif/
http://img829.imageshack.us/i/72979235.gif/
Kenneth Eaton
Kenneth Eaton el 25 de En. de 2011
Pepa, I'm still not clear on exactly what error/problem you are having when you run your code. The two screenshots of the variables look fine. Note that kunn(:,:,1) and jaj are the same, there is just a scaling factor of 1.0e+003 used to display kunn.
Pepa
Pepa el 25 de En. de 2011
My problem is that if i put in kunn(:,:,X) X = 1,2,...n, In jaj is still kunn(:,:,1).
Walter Roberson
Walter Roberson el 25 de En. de 2011
By the way, preallocation of jaj is not needed as you are overwritten its complete contents each trip through the loop.
Walter Roberson
Walter Roberson el 25 de En. de 2011
Pepa, put in kunn(:,:,X) *where* ?? Code example, please.
Pepa
Pepa el 25 de En. de 2011
Whole code of function:
function [F0mean,F0max,F0min,F0max_min,F0std,F0median,relF0VR,relF0SD,...
Emean,Emax, Emin,Emax_min, Estd, Emedian, TEO,...
Jmean,Jmax,Jmin,Jmax_min,Jstd,Jmedian,...
Smean,Smax,Smin,Smax_min,Sstd,Smedian,...
F1mean,F1max,F1min,F1max_min,F1std,F1median,...
F2mean,F2max,F2min,F2max_min,F2std,F2median] = HRA ()
[wav,lab]=databaze;
wav = wav';
lab = lab';
L = length(wav);
for j = 2:L+1
brd = process_lab_file(lab{j-1});
label = brd(:,1);
K = length(label);
for i = 1:K
[F0mean,F0max,F0min,F0max_min,F0std,F0median,relF0VR,relF0SD,...
Emean,Emax, Emin,Emax_min, Estd, Emedian, TEO,...
Jmean,Jmax,Jmin,Jmax_min,Jstd,Jmedian,...
Smean,Smax,Smin,Smax_min,Sstd,Smedian,...
F1mean,F1max,F1min,F1max_min,F1std,F1median,...
F2mean,F2max,F2min,F2max_min,F2std,F2median]=extr_part_of_sig(wav{j-1},label(i),lab{j-1});
priznaky = [F0mean,F0max,F0min,F0max_min,F0std,F0median,relF0VR,relF0SD,...
Emean,Emax, Emin,Emax_min, Estd, Emedian, TEO,...
Jmean,Jmax,Jmin,Jmax_min,Jstd,Jmedian,...
Smean,Smax,Smin,Smax_min,Sstd,Smedian,...
F1mean,F1max,F1min,F1max_min,F1std,F1median,...
F2mean,F2max,F2min,F2max_min,F2std,F2median];
O = length(priznaky);
for o = 1:O
zap(o,i) = priznaky(o);
end
end
F = length(priznaky);
for i = 1:K
for a = 1:F
kunn(a,j-1,i) = zap(a);
end
end
end
S = length(label);
jaj = zeros(39,17);
for f = 1:S
jaj = kunn(:,:,2); % PROBLEM IS HERE
koj = jaj';
%xlswrite('ZK.xls', koj, label{i}, 'B2');
end
Oleg Komarov
Oleg Komarov el 25 de En. de 2011
Why don't you apply what Kenneth suggested (not to mention Walter):
jaj = kunn(:,:,s)
And what's the problem, btw, can you elaborate, post any error message or unwanted result?
Pepa
Pepa el 25 de En. de 2011
It does't work, I applied this jaj=kunn(:,:,s).I don't know why. No error, just in the jaj is still only first matrix.
Pepa
Pepa el 25 de En. de 2011
I can use every number on position 's' and result is always same and this is first matrix...
Oleg Komarov
Oleg Komarov el 25 de En. de 2011
isequal(kunn(:,:,1),kunn(:,:,2)) should be 0, can you confirm?
Pepa
Pepa el 25 de En. de 2011
No i can't. isequal(kunn(:,:,1),kunn(:,:,2)) result is logical 1. And this is my problem.

Iniciar sesión para comentar.

Categorías

Productos

Etiquetas

Preguntada:

el 24 de En. de 2011

Community Treasure Hunt

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

Start Hunting!

Translated by