Resizing matrixes in MATLAB

조회 수: 1 (최근 30일)
Paul Case
Paul Case 2018년 1월 4일
댓글: Jan 2018년 2월 5일
Hello, I currently want to reshape an array from a 53 by 1 matrix to a 53 by 19 matrix here is the code so far:
function finalout = addteams(inputFile, OutputFile, rowSize)
OutputFile = [OutputFile newcol];
for i = drange(1:rowSize)
team = inputFile(i,1);
outRowSize = size(OutputFile);
pos = find(inputFile == team);
OutputFile = reshape(OutputFile, [outRowSize, 2]);
OutputFile(pos,2)= OutputFile(pos,2) + inputFile(i,2);
end
end
the outputfile comes in as a 53 by 1 matrix. I want to take variables from the input file(342X18) and add up the teams in order to create a set of unique teams.
  댓글 수: 3
Eric
Eric 2018년 2월 5일
편집: Eric 2018년 2월 5일
Some more comments/questions on top of Ben's:
  • What is newcol?
  • If it is always true that rowSize == size(inputFile,1), you don't need to pass it as an input. You can directly say for i=drange(1:size(inputFile,1))
  • Line 5 returns a (1x2) vector, which ultimately causes an error in line 7 since you are trying to reshape to twice the number of elements you have. Having said that, why do you need to reshape OutputFile at all? It appears to do effectively nothing...
  • If inputFile is (342x18), using find without indexing will potentially return a value larger than any dimension of OutputFile or inputFile, causing an error when you try to use it later. Since you are also later using pos to index into Outputfile, perhaps you meant pos = find(OuputFile(:,1)==team)?
  • finalout is not set in the function you provided...
  • "Add up the teams" doesn't make sense, unless you are trying to add some stats about some teams in columns 2-18. If you are just trying to "create a set of unique teams", why not just use unique(inputFile(:,1))? (This goes back to the question of what are you trying to do?)
Jan
Jan 2018년 2월 5일
If the inputs are not files, the names "In" and "Out" are much better to read than "inputFile" and "outputFile".
This must fail:
outRowSize = size(OutputFile);
OutputFile = reshape(OutputFile, [outRowSize, 2]);
This would try to reshape the matrix with the size [x,y] to a 3D array with the size [x,y,2]. But the latter has twice as much elements.
I still do not understand, what you want to calculate.

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

답변 (0개)

카테고리

Help CenterFile Exchange에서 File Operations에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by