0

I want to make an array 15*15 and fill them with these code and I want to find max of a row of it. I wrote this codes in MATLAB to make an array:

a = zeros (15) - inf;

for i=1:15 
    k2=4;
    l2=1;

    k=floor((i-1)/3);
    l=mod((i-1),3);
    f=false;
    if (k==k2 && abs(l2-l)==1)
        f=true;
    end
    if(l==l2 && k2-k==1)
        f=true;
    end
    if(k2-k==1 && abs(l2-l)==1)
        f=true;
    end

    if (f)
        a(i,14)=100;
    end
end
max=200;
for i=1:15
    if(find(2,i) < max)
        max=i;
    end
end

max=0

when I wrote these codes to find maximum index in 2nd row of array this error shown:

b=a(2,:)

b =

     1  -Inf     1     1     1     1  -Inf  -Inf  -Inf  -Inf  -Inf  -Inf  -Inf  -Inf  -Inf

>> [~,index]=max(b)
??? Indexing cannot yield multiple results.
6
  • Isn't there something missing in the first line? Commented Feb 28, 2013 at 7:15
  • yes , after zeros ... I fill array (a) with -inf by 2 for loop. Commented Feb 28, 2013 at 7:18
  • BTW you can create a matrix of -Inf just by subtracting inf from the zeros matrix so you don't need the loops. I have edited your code to reflect this. Commented Feb 28, 2013 at 7:24
  • @Dan: what is your edit of the code good for? The line zeros (15,15) - inf; does not make sense at all when the created array is not assigned to a variable. Commented Feb 28, 2013 at 8:47
  • @H.Muster Yes you are correct. I did not notice that the original question just had zeros assigned to nothing. It should be a = zeros(15) - inf; based on the OPs comment above. Commented Feb 28, 2013 at 9:00

1 Answer 1

3

You have variable max and trying to use function max.

It's a good practice to check for existing names with exist var_name or which var_name command.

Rename your variable max in the code and remove it from the workspace with clear max.

Sign up to request clarification or add additional context in comments.

3 Comments

What to explain. You define variable in max=200. Then trying to call function max. In MATLAB variable indexing and function call have the same syntax. Function can produce multiple outputs, but indexing cannot.
Matlab doesn't know if you want to use the function max() or the variable you created called max. Basically just give your variable max a different name.
@Dan: yes, forgot to mention this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.