How to output function results over split columns

12 visualizaciones (últimos 30 días)
Bob Thompson
Bob Thompson el 24 de En. de 2019
Comentada: TADA el 29 de En. de 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 comentarios
Bob Thompson
Bob Thompson el 24 de En. de 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 el 24 de En. de 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.

Iniciar sesión para comentar.

Respuesta aceptada

TADA
TADA el 24 de En. de 2019
Editada: TADA el 24 de En. de 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 comentarios
Bob Thompson
Bob Thompson el 29 de En. de 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 el 29 de En. de 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

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by