creating matrix out of another matrix

I have a results matrix of size 104*14. I want to create a matrix of size 8*13 in which u(1,1)=results((1:13),4) and....I tried to write the matrix like:
u_mean_Uund=[results(1:13),4; results(14:26),4;results(27:39),4;results(40:52),4;results(53:65),4;...
results(66:78),4;results(79:91),4;results(92:104),4]
But this shows the first column of the results matrix, I actually want a 8*13 matrix in which the arrays are all from column 4.

2 Comments

Your description contradicts itself: "I want to create a matrix of size 8*13..." but also "I actually want a 8*14 matrix...": so do you want an 8x13 or an 8x14 matrix?
Check my edited answer.

Sign in to comment.

 Accepted Answer

I solved it using following for loop:
for nn=1:8
u_depth(nn,:)=transpose(results((13*(nn)-12):13*(nn),4));
end

4 Comments

Bruno Luong
Bruno Luong on 9 Sep 2019
Edited: Bruno Luong on 9 Sep 2019
This gives exactly Stephen's answer, and your code is hard to read and slow because u_depth is not preallocated.
You should not accept your own answer, especially there is a better solution proposed to you before.
If I could I would give you a negative vote.
I explained why the older comment doesn't give the answer...and i also appreciate that...i also have used zeros matrix to solve the preallocation problem in my original code...i just wanted to mention the part that was my question I didn't know accepting my own answer is contrary to the rules...if it is i will unaccept that I accepted my answer to show that the solution was found and worked properly
"I explained why the older comment doesn't give the answer"
And Stephen has explained to you the opposite and I tested it
results=rand(104,14);
% your solution
for nn=1:8
u_depth(nn,:)=transpose(results((13*(nn)-12):13*(nn),4));
end
% Stephen's solution
out = reshape(results(:,4),13,8).';
isequal(u_depth,out)
It returns 1, meaning both gives the same ANSWER on a random input. So Stephen is right and you are wrong.
You just are not rigouruous person and lack patient or perhaps a bit of courtesy to discuss with Stephen.
Well... I dont know niether you nor Stephen and I dont want to argue with anyone...i was rushing to get the answer as soon as possible, so I also worked on that... Anyway i think you cant judge people like this...and again I thanks everyone for their answers and their help.

Sign in to comment.

More Answers (1)

Stephen23
Stephen23 on 9 Sep 2019
Edited: Stephen23 on 9 Sep 2019
out = reshape(results(:,4),13,8).';
out(:,14) = 4; % if you want an 8x14 matrix

10 Comments

This also gives me the values in column 1.
I want a code that can divide my result(104*14) matrix to 8 rows in which all the columns are arrays in column 4 of result matrix. (Extract values in column 4 for each 13 rows (it will be eight set of values) and put them in 8 rows).
@mehra : see my editted answer: it only takes data from the fourth column,
Thank you @Stephen Cobeldick,
actually ıt should be
out=reshape(results(:,4),8,13) since İ need 8 rows and 13 columns but
I think the reshape command wouldnt work here because in reshape it takes the first 8 data and then the next 8 data and... in fact I want the first 13 data of column 4 to make my 1st row which includes 13 columns and then the next 13 data should build my next row containing 13 columns and so on...
In fact I want to get the following lines in a shorter way or by a command:
u_mean_1=transpose(results((1:13),4));
u_mean_2=transpose(results((14:26),4));
u_mean_3=transpose(results((27:39),4));
u_mean_4=transpose(results((40:52),4));
u_mean_5=transpose(results((53:65),4));
u_mean_6=transpose(results((66:78),4));
u_mean_7=transpose(results((79:91),4));
u_mean_8=transpose(results((92:104),4));
each u_mean_i should make one row with 13 columns
how can I write it in a shorter way and in one copact code?
Stephen23
Stephen23 on 9 Sep 2019
Edited: Stephen23 on 9 Sep 2019
"actually ıt should be out=reshape(results(:,4),8,13) since İ need 8 rows and 13 columns but"
No it shouldn't be.
MATLAB stores arrays in column-major order, which is why I reshaped to size (13,8) and then used transpose. You totally ignored the transpose I used, which means that you will get all of your data going down the columns first... which is definitely not what you requested.
"I think the reshape command wouldnt work here because in reshape it takes the first 8 data and then the next 8 data ..."
Actually reshape works fine, exactly the way I used it in my answer.
They way you used it will not work (in exactly the way that you describe).
"in fact I want the first 13 data of column 4 to make my 1st row which includes 13 columns and then the next 13 data should build my next row containing 13 columns and so on"
Which is exactly what the code in my answer does (note: this is not the same as your code).
"how can I write it in a shorter way and in one copact code?"
I would use the code in my answer (not your incorrect code).
I'll try your edited code too...I may have ignored sth...but my code works properly too...maybe my explanation was not clear... But it works ok If the problem is accepting your answer I will do that, no matter ? Thanks
Stephen23
Stephen23 on 9 Sep 2019
Edited: Stephen23 on 9 Sep 2019
"...If the problem is accepting your answer"
This is a public forum and I am not just writing for you but for all future potential readers: if you make a statement that is incorrect regarding my answer and reshape usage than I am not going to let other readers be mislead by your incorrect statements.
"...maybe my explanation was not clear"
Why do you write that? I find your original question was clear enough.
I am happy that my explanation seemed clear for you...but still you have not seen my problem thats why i thought my explanation may have been not enough..anyway Its good that you are thinkg about future readers ill appreciate that too And It would be better if we do less accusation and arguments.
"...but still you have not seen my problem..."
If I failed to understand your problem as you claim, how did I write code that returns exactly the same result as your loop (which you posted four hours later)?
>> results = rand(104,14);
>> for nn=1:8 % your loop
u_depth(nn,:)=transpose(results((13*(nn)-12):13*(nn),4));
end
>> out = reshape(results(:,4),13,8).'; % my simpler reshape
>> isequal(u_depth,out) % The same outputs!
ans =
1
That would require me to make two mistakes:
  1. to misunderstand your question (which is certainly possible),
  2. then to write strangely buggy MATLAB code that doesn't do what I want it to do (based on my incorrect understanding of your problem), but that coincidentally ends up doing exactly the same thing as your loop....
With such luck, I should go an buy a lotto ticket!
Anyway i am happy that my solution also works

Sign in to comment.

Tags

Asked:

on 9 Sep 2019

Commented:

on 9 Sep 2019

Community Treasure Hunt

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

Start Hunting!