Error in OCTAVE code

I just started programming in OCTAVE and my code presented the following error:

Function code

Commands and function call

>> A = imread('cameraman.tif');
>> B = imread('circuit.tif');
>> D = sub_img(A,B);

Error:

Warning: suggest parenthesis around assignment used as truth value near line 4, column 15 in file 'C:\Users\rafae\Octave\0-intro-matlab-octave\sub_img.m '

Error: in computed assignment A (index) OP = X, a must be defined first

Error: called from sub_img at line 4 column 3

With this, I tried other sources of research to find the solution, but none served me. I appreciate the attention

 0
Author: JeanExtreme002, 2020-02-22

2 answers

Good after ederwander's help. I was able to come up with other solutions and manage to leave the "correct" code at first.

After the correction made the code printed what was to be printed 'images are not the same size'. But another error appeared error: value on right hand side of assignment is undefined. To correct it it was necessary to put a value for the variable C (C = 0) the function C = sub_img(A,B). With this the function code was:

Changed code

Then after use it he sent the message, but I realized that the code would never leave if because the condition

Script (it is worth noting that the script has a different task than the function, I just took advantage of even the resize command):

Clear; % clears variables

A = imread ('cameraman.tif'); % reads first image

B = imread ('circuit.tif'); % reads second image

Imshow (A); pause(3); % shows the first image and waits 3 seconds imshow (B); pause(3); % shows the second image and waits 3 seconds

B = imresize(B, size (a)); %Changes image size B to image size a

C = imadd (A,B); imshow (C); % shows summation 8 Bits

Two images with their pixels subtracted

Well, at first my problem was solved, and I would like to ask if anyone has any book, source, file to indicate to study programming in octave I thank.

Thank You Ederwander!

 1
Author: Rafael Sponchiatto, 2020-06-11 14:45:34

I think you have a syntax error in your function, it does not exist or I have at least never seen this type of operator in octave / matlab -= I think you want to compare if the dimensions of A and B are equal so you should use the operator ==, the first Warning that appeared in your code was pq vc did not add more parentheses in your if follows a simple test with the above observations running in the Octave without errors:

A = [1 2 3]
B = [1 2 3]
if ((size(A) == size(B)))
    disp("oi")
end
 0
Author: ederwander, 2020-02-22 22:06:48