How to output function results over split columns

12 Ansichten (letzte 30 Tage)
Bob Thompson
Bob Thompson am 24 Jan. 2019
Kommentiert: TADA am 29 Jan. 2019
I have a loop which contains a function that produces results to a different sheet of a matrix. I would like to insert an additional column of information after the function has been completed, but I am not sure how to index the function output to allow for this. Ideally, I would like to collect the output information into a split set of columns so my inserted column is in the middle, but I either receive a 'Subscripted assignment dimension mismatch' or 'Undefined function or variable 'output'' error (depending on if I initialize with a blank array or not).
for i = 1:N;
output(:,[1:4,6:end],i+1) = processFiles(input);
output(:,5,i+1) = output(:,4,i+1)./output(:,2,i+1);
end
Does anybody have a suggestion on how to go about doing this type of insertion?
(Before you ask, no, I cannot provide a sample of 'input' or the 'processFiles' function)
Thanks
  3 Kommentare
Bob Thompson
Bob Thompson am 24 Jan. 2019
That would work, I was hoping to avoid making a temporary variable though. If I can't then that's the end of it, but for simplicity's sake I would prefer only having one matrix.
TADA
TADA am 24 Jan. 2019
I'm Not Sure If It's Possible Or Not, I Never Tried This Type Of Indexing. But I Beg To Differ About "Simplicity's Sake".
The Complex Indexing Seems Less Clear Than Doing It With The Intermediate
Think About The Next Person Reading Your Code, or Yourself Reading It A Year From Today.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

TADA
TADA am 24 Jan. 2019
Bearbeitet: TADA am 24 Jan. 2019
Figured it out...
the problem is you use the end operator with a matrix that's not asigned yet
or when you do preset it to a different size the end operator returns a value thats not large enough to accomodate your output matrix (or too big for that matrix, still mismatching)
Here is my mock code:
clear;
n = 8;
N = 3;
output = zeros(n, n+1, N+1);
for i = 1:N
output(1:8, [1:4,6:end], i+1) = processFiles(n);
output(:,5,i+1) = output(:,4,i+1)./output(:,2,i+1);
end
output
function out = processFiles(in)
out = repmat(1:in,in,1);
end
Now, when you change the preallocation line:
clear output; % output not predefined, end operator throws Undefined function or variable 'output'
output = zeros(8,9,4); % the sizes generated by the mock code - works fine.
output = zeros(0,9,0); % will reallocate the matrix each iteration
% but works fine because the end operator is used only on the x dimention
output = zeros(0,0,0); % or an empty matrix [] - throws Subscripted assignment dimension mismatch error
% because 6:end returns an empty array
I'm guessing you have a problem with the preallocation, fix that and your indexing will probably work...
As for simplisity, that's completely up to you of course...
  4 Kommentare
Bob Thompson
Bob Thompson am 29 Jan. 2019
Hmm, were you able to use the following?
output(:,[1:4,6:end],i+1) = processFiles(input);
Even with preallocation of (0,9,0) I received a "Subscripted assignment dimension mismatch." Further investigation revealed this to be because of the ':' in the row element definitions. I have found a way to preallocate this with a parameter from elsewhere in my input, but I am curious to know if you were able to complete assign the output without defining specific rows in the output call.
TADA
TADA am 29 Jan. 2019
sorry, I changed your colon into a vector 1:8, thats the difference...
the colon operator alone is all the indices in that dimention, so when the size is 0, it's like writing 1:0
thats the source of that error, I didn't notice that before... again a good preallocation will solve that issue

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Matrix Indexing 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