How to output function results over split columns

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 commentaires

TADA
TADA le 24 Jan 2019
Set The Output Of processFiles Into A Matrix Regularly, Then Concat All Three Parts Of The Data Into Your Output Matrix
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 le 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.

Connectez-vous pour commenter.

 Réponse acceptée

TADA
TADA le 24 Jan 2019
Modifié(e) : TADA le 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 commentaires

Hmm, so if I understand this correct, then preallocation is the key.
How does output = zeros(0,9,0) work? When I tried this I ended up with a blank array, because of the zero size dimentions. Is this something that was added more recently than 2016a?
TADA
TADA le 29 Jan 2019
I currently have 2017b, although i'm pretty sure it should work the same
it is an empty array of size 0x9x0
in the above demo, this array is reallocated each iteration,
initial size: [0 9 0]
size after iteration #1: [8 9 2]
size after iteration #2: [8 9 3]
size after iteration #3: [8 9 4]
so this is generally not a good idea, but it works because the end operator used on the 2nd dimention provides the correct index - 9
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 le 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

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by