Resizing matrixes in MATLAB

1 view (last 30 days)
Paul Case
Paul Case on 4 Jan 2018
Commented: Jan on 5 Feb 2018
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 Comments
Eric
Eric on 5 Feb 2018
Edited: Eric on 5 Feb 2018
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 on 5 Feb 2018
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.

Sign in to comment.

Answers (0)

Categories

Find more on File Operations in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!