Unknown behavior of ceil() function (Matlab 2017b)
8 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hanns Michel
am 7 Feb. 2019
Bearbeitet: Hanns Michel
am 8 Feb. 2019
Dear Matlab community,
I have ran into an issue while using the ceil()-function and it results in an "Not enough input arguments" error.
I find it difficult to described behavior because I cannot find the logic behind it, but maybe I'm missing something. So I prepared three examples to demonstrate.
The question is: Why does Variant 3 not work?
You can even "pause/debug" Variant 3 in those comment lines before the last call (uncomment disp() first) and copy the last two lines with the last call into the command window and it works. If you than continue it still results in an error.
Variant1: Works and ceil() is called three times. First with the keyword "end", then with the variable "x" and again with the keyword "end".
clear all
%% Variant 1
x = 4; y = 5; Test = zeros(x,y);
MAGIC1 = magic(6);
RESULT_1 = MAGIC1(ceil(end/2),:) % Keyword end
Test(ceil(x/2),2) = 1; % Same as in variant 2, Variable x
MAGIC2 = magic(6);
RESULT_2 = MAGIC2(ceil(end/2),:) % Keyword end
Variant2: Works as well, but the first call is commented, the second is changed to be called with keyword "end". The third call remains unchanged.
clear all
%% Variant 2
x = 4; y = 5; Test = zeros(x,y);
% MAGIC1 = magic(5); % not needed in variant 2
% RESULT_1 = MAGIC1(ceil(end/2),:) % not needed in variant 2
Test(ceil(end/2),2) = 1; % to index x, x changed to end
MAGIC2 = magic(6);
RESULT_2 = MAGIC2(ceil(end/2),:)
Variant3: This one results in an error. The first call is again commented (but by uncommenting it you can make it work, (lines 5,6)). The second call is by variable x. and the third on stays the same as before (keyword end)
clear all;
%% Variant 3
x = 4; y = 5; Test = zeros(x,y);
% MAGIC1 = magic(5); % by commenting these lines
% RESULT_1 = MAGIC1(ceil(end/2),:) % line 14 results in error
Test(ceil(x/2),2) = 1; % same as in variant1
% disp('Debug here and copy following lines to workspace should work as well')
% Continue to run and it will result in "Not enough input arguments."
MAGIC2 = magic(5);
RESULT_2 = MAGIC2(ceil(end/2),:)
Thanks to all who help me unpuzzle this
Hanns
0 Kommentare
Akzeptierte Antwort
Image Analyst
am 7 Feb. 2019
You can't use ceil(end/2). Even though the result of ceil is going to be used as an index, that index cannot be created from ceil(end/2) because it doesn't know what "end" is, even though you might think it should since the "end" is also inside a matrix reference. To fix, replace "end" by something, like size(MAGIC2, 1) or 3 or something:
RESULT_2 = MAGIC2(3,:)
6 Kommentare
Guillaume
am 8 Feb. 2019
You would expect mymatrix(1:end-1) to work. Yet, in theory, this is actually, mymatrix(1:minus(end, 1)), so the end is inside a function call. It really shouldn't complicate anything for the parser if the end is inside a function call (as long as that call can't be confused with a matrix indexing). And actually, I'm surprised that variant 3 does not work properly in R2017b. Unfortunately, I no longer have that version installed to test.
Weitere Antworten (0)
Siehe auch
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!