I have a matrix 'm' of oder 4 by6by960. I want to extract 1st and 3rd dimensions, i.e 4by960. how to do this?

1 Ansicht (letzte 30 Tage)
m=rand(4,960);
m = [m; zeros(20,960)];
m=permute(reshape(m, 4,6,960), [1 2 3]); % need this 3d matrix for some operation
m1=squeeze(m(:,6,:));
when i use above line to get my requirement. it returns a matrix m1 of 4by960 with all zero enteries but i need to extract non-zero elements in 4by960 dimensions. kindly help to resolve the issue

Akzeptierte Antwort

Stephan
Stephan am 11 Dez. 2018
Bearbeitet: Stephan am 11 Dez. 2018
Hi,
all the non-zero elements are in column 1 - not in column 6:
m=rand(4,960);
m = [m; zeros(20,960)];
m=permute(reshape(m, 4,6,960), [1 2 3]); % need this 3d matrix for some operation
m1=squeeze(m(:,1,:));
Sometimes for debugging it is helpful to have a look to the results that your code produces - See here for m after performing the permute and reshape:
val(:,:,1) =
0.5108 0 0 0 0 0
0.5300 0 0 0 0 0
0.1545 0 0 0 0 0
0.2341 0 0 0 0 0
val(:,:,2) =
0.6191 0 0 0 0 0
0.3090 0 0 0 0 0
0.2135 0 0 0 0 0
0.2826 0 0 0 0 0
val(:,:,3) =
0.5438 0 0 0 0 0
0.7191 0 0 0 0 0
0.0393 0 0 0 0 0
0.6480 0 0 0 0 0
You can continue this up to 960 - but the result keeps the same. So now you know the reason for your problem, but what do you want to achieve with permute? Note that:
>> A = [1 2 3; 4 5 6; 7 8 9]
A =
1 2 3
4 5 6
7 8 9
>> B = permute(A, [1 2 3])
B =
1 2 3
4 5 6
7 8 9
In other words - your permute does not do anything - if you leave the permute command away the resulting matrix m keeps the same:
m1 = permute(reshape(m, 4,6,960), [1 2 3]);
m2 = reshape(m, 4,6,960);
sum(sum(sum(m1 == m2))) == 4*6*960
ans =
logical
1
This seems to be the original problem you have - your permute call does not change your matrix the way that you expect it to do.
Best regards
Stephan

Weitere Antworten (0)

Kategorien

Mehr zu Resizing and Reshaping Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by