# obtain specific components of a matrix

5 Ansichten (letzte 30 Tage)
mehra am 25 Okt. 2019
Kommentiert: mehra am 31 Okt. 2019
Hello
I have a TKE_t matrix with dimensions 104*19000 , and I need to obtain values in the following order
1 14 27 40 53 66 79 92
2 15 28 41 54 67 80 93
3 16 29 42 55 68 81 94
......
13 26 39 52 65 78 91 104
ı can obtain the first one by following code:
TKE_d1=zeros(8,1);
for r=1:8
TKE_d1(r,:)=TKE_t(13*r-12,:);
end
and I can repeat the same code by changing 13*r -12 to 13*r-11 and so on to obtain other values but how I can write one code to do this ? I thought of a 2 varaible for-loop but ı couldnt make it work...
##### 0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Akzeptierte Antwort

the cyclist am 30 Okt. 2019
Bearbeitet: the cyclist am 30 Okt. 2019
M = 8;
N = 13;
L = 19000;
TKE_d = reshape(permute(reshape(TKE_t,N,M,L),[2,1,3]),M*N,L);
where
TKE_t=transpose(TKE);
as you defined in your code. I defined those parameters because I was testing on smaller cases, and also to illustrate how it generalizes.
##### 1 Kommentar-1 ältere Kommentare anzeigen-1 ältere Kommentare ausblenden
mehra am 31 Okt. 2019
this worked, thank you

Melden Sie sich an, um zu kommentieren.

### Weitere Antworten (3)

the cyclist am 25 Okt. 2019
Bearbeitet: the cyclist am 25 Okt. 2019
TKE_d = reshape(TKE_t,[13 8 19000]);
This will result in a 3-dimensional array, each "slice" of which is like one of the matrices you want to create. Then
TKE_d(1,:,:)
will be
TKE_t([1 14 27 40 53 66 79 92],:)
(but you may need to reshape again to get what you want).
##### 4 Kommentare2 ältere Kommentare anzeigen2 ältere Kommentare ausblenden
mehra am 29 Okt. 2019
I actually want to do what you said in fact break 104 data into 8*13 matrix but each array in this matrix is a 1*19000 matrix namely i need a 3d matrix but I want to know how can I make it by for-loop. I could use for-loop to obtain each 8*19000 matrixes(blocks) but I have to write this for-loop for 13 times to get the data. In another way : I already have 104*19000 values ordered from 1 to 104 subsequently now i want the same data but in other order: 1st to 8th rows is values 1 14 27 40 53 66 79 92 of the original matrix the 2nd 3rd and other rows also should be in this order...consequently the last row will be values 13 26 39 52 65 78 91 of the original matrix.... In fact my final matrix will be again 104*19000 matrix containing 13 "blocks" of 8*19000 matrix in the order I explained. Please let me know if it needs more explanation.
the cyclist am 29 Okt. 2019
Your question continues to be unclear to me. Here are things that are not clear:
• What size/shape is your input? Is it a 104x19000 matrix?
• How many variable are in the output? Just one variable? Or more than one variable?
• What is the size/shape of the output variable(s)?
I still think you might just be able to use the reshape command, and maybe the transpose command?
You seem to have ignored my suggestion to use my example of a very small array:
TKE_t = reshape(1:30,[6 5])
TKE_t =
1 7 13 19 25
2 8 14 20 26
3 9 15 21 27
4 10 16 22 28
5 11 17 23 29
6 12 18 24 30
This is a 6x5 matrix (instead of your 104x19000 matrix). But can you explain EXACTLY what you want for output, if this was the input? Or maybe that doesn't make sense.

Melden Sie sich an, um zu kommentieren.

mehra am 30 Okt. 2019
my input is 104*19000 matrix (it can also be 19000*104, doesnt change the result)
I think we can ignore the second dimension (19000), because what I want to change is the first dimension of the matrix.
therefoıre I can explain like this:
I have 104 data that starting from 1 (in one dimension) continous up to 104. I want to reshape these data in a second matrix in a way that the first 8 data would be values 1 14 27 40 53 66 79 92 of the original matrix, the second 8 data should be values number 2 15 28 41 54 67 80 93 of the original matrix and it should go like this to the end. In fact I need to reshape the matrix, but what makes this difficult is the 3rd dimension which I ignored here just to explain the issue (in fact each of the values have a second dimension of 19000) ...so my output matrix will still be 104 *19000 but in a different order...
I used the following code but I have to repeat it for 13 times (which is similar to the one I wrote in the previous comment)
TKE_t=transpose(TKE);
TKE_d1=zeros(8,19000);
for r=1:8
TKE_d1(r,:)=TKE_t(13*r-12,:);
end
%% for second 8 values it will be:
TKE_d2=zeros(8,19000);
for r=1:8
TKE_d2(r,:)=TKE_t(13*r-11,:);
end
%% with 10 more for-loops I can produce 13 matrixes overall in the way I want
If I can write one command that can give these 13 matrixes in one matrix that could help me
thanks
##### 0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

mehra am 30 Okt. 2019
This is what my final (but long) code looks like
TKE_t=transpose(TKE);
TKE_d1=zeros(8,19000);
for r=1:8
TKE_d1(r,:)=TKE_t(13*r-12,:);
end
TKE_d2=zeros(8,19000);
for r=1:8
TKE_d2(r,:)=TKE_t(13*r-11,:);
end
TKE_d3=zeros(8,19000);
for r=1:8
TKE_d3(r,:)=TKE_t(13*r-10,:);
end
TKE_d4=zeros(8,19000);
for r=1:8
TKE_d4(r,:)=TKE_t(13*r-9,:);
end
TKE_d5=zeros(8,19000);
for r=1:8
TKE_d5(r,:)=TKE_t(13*r-8,:);
end
TKE_d6=zeros(8,19000);
for r=1:8
TKE_d6(r,:)=TKE_t(13*r-7,:);
end
TKE_d7=zeros(8,19000);
for r=1:8
TKE_d7(r,:)=TKE_t(13*r-6,:);
end
TKE_d8=zeros(8,19000);
for r=1:8
TKE_d8(r,:)=TKE_t(13*r-5,:);
end
TKE_d9=zeros(8,19000);
for r=1:8
TKE_d9(r,:)=TKE_t(13*r-4,:);
end
TKE_d10=zeros(8,19000);
for r=1:8
TKE_d10(r,:)=TKE_t(13*r-3,:);
end
TKE_d11=zeros(8,19000);
for r=1:8
TKE_d11(r,:)=TKE_t(13*r-2,:);
end
TKE_d12=zeros(8,19000);
for r=1:8
TKE_d12(r,:)=TKE_t(13*r-1,:);
end
TKE_d13=zeros(8,19000);
for r=1:8
TKE_d13(r,:)=TKE_t(13*r,:);
end
TKE_d=[TKE_d1 ;TKE_d2; TKE_d3; TKE_d4; TKE_d5; TKE_d6; TKE_d7; TKE_d8; TKE_d9; TKE_d10; TKE_d11; TKE_d12; TKE_d13];
##### 0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Kategorien

Mehr zu Loops and Conditional Statements 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