creating matrix out of another matrix

조회 수: 6 (최근 30일)
mehra
mehra 2019년 9월 9일
댓글: mehra 2019년 9월 9일
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
Stephen23
Stephen23 2019년 9월 9일
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?
mehra
mehra 2019년 9월 9일
Check my edited answer.

댓글을 달려면 로그인하십시오.

채택된 답변

mehra
mehra 2019년 9월 9일
I solved it using following for loop:
for nn=1:8
u_depth(nn,:)=transpose(results((13*(nn)-12):13*(nn),4));
end
  댓글 수: 4
Bruno Luong
Bruno Luong 2019년 9월 9일
편집: Bruno Luong 2019년 9월 9일
"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.
mehra
mehra 2019년 9월 9일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Stephen23
Stephen23 2019년 9월 9일
편집: Stephen23 2019년 9월 9일
out = reshape(results(:,4),13,8).';
out(:,14) = 4; % if you want an 8x14 matrix
  댓글 수: 10
Stephen23
Stephen23 2019년 9월 9일
"...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!
mehra
mehra 2019년 9월 9일
Anyway i am happy that my solution also works

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by