Resize a vector in Matlab

Asked

Viewed 117 times

4

Good afternoon. Here’s the following, I used the following function to create an empty array that later stores certain values requested from the user:

fragil = zeros(1, 10); %criaçao do vetor
idF = str2double(input('Insira um número inteiro!: ', 's')); % pedido de input
fragil(1,k)=idF; %guarda o valor no vetor

However, when the number of inserted values is less than 10, the array will stay with the remaining values at 0 and then when I try to fetch the values of the array to use in a comparison I have the following error message:

Index in position 2 is invalid. Array indices must be Positive integers or Logical values.

Error in Step 0 (line 40)

if Mcobertura(m,elder)=1

My doubt consists (or not) in two parts: 1- How can I "clear" the null values of positions not used in the vector. 2-If the error message is not associated with this detail, what will be the problem. Then there is the code snippet where the error is identified.

for l=1:k
    verificaF=0;
    idosoF= fragil(1,l);
    for m=1:numAmbulancias
        if MCobertura(m,idosoF)==1
            verificaF=verificaF+1;
        end
    end
    if verificaF<2
        haSolucao=false;
        break;
    end

In which k corresponds to the number of inputs made by the user, in Ambulatory corresponds to the lines of Mcobertura and aged.

1 answer

2


The first question is relatively simple to solve.

If you are sure to have non-zero values in your idF, This becomes very simple. Just use a comparison to Matrix as an index. Below I put some examples of Matrix fragil and how the indices behave. Use the last line of code below before moving to the next part.

fragil =
     5     3     0     1     0     0     0     0     0     0
fragil~=0
ans =
     1     1     0     1     0     0     0     0     0     0
fragil(fragil~=0)
ans =
     5     3     1
%%% PARTE RELEVANTE DO CÓDIGO IMPORTANTE
fragil=fragil(fragil~=0)
fragil =
     5     3     0     1

If you have to accept zero as inputs, you need to place an input counter. But I think you already have that, because you use k in the first part of the code, but this is not stated in the above functions. Since it is the number of inputs, just use

fragil =
     5     3     0     1     0     0     0     0     0     0
k =
4
fragil=fragil(1,1:k)
fragil =
     5     3     0     1

The second question revolves around:

Array indices must be Positive integers or Logical values.

This error says that you are placing a scalar value as the Matrix index. This means you may be putting a non-integer value (e. g. 1.2) or else 0. See that your input in

idF = str2double(input('Insira um número inteiro!: ', 's')); % pedido de input

does not eliminate the possibility of someone putting a real value (e. g. 3.14). A solution would be to use round(str2double(input())) to make sure they are whole and use my previous solution to remove the zeros.

Otherwise it becomes difficult to test your code, because you are placed in a way depending on the inputs, be they 0 or not, you can limit it by the value of k, but if the input is 0, it will give problems. To solve this, you need to change your method.

If the code has k=1 and each time it has an input k=k+1, after the last input, the value will be equal to número_de_inputs + 1 . If this is the case, just subtract 1 from k when there are no more inputs.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.