0

I am trying to categorise the years based on some conditions. If any year has rainfall less than a specific number it will indicate as a dry year. I have tried the following, but it gives me error "In an assignment A(I) = B, the number of elements in B and I must be the same."

The code is

Year_Category = zeros(ny,1);
for i = 1:ny;
    if (xy(i)< Lower_Limit)
        Year_Category(i) = 'Dry';
    elseif (xy(i)> Upper_Limit)
        Year_Category(i) = 'Wet';
    else
        Year_Category(i) = 'Average';
    end
end

Any help would be appreciated.

Best regards

1 Answer 1

2

You are trying to assign characters to a numeric array. That's why you're getting a dimension mismatch. Each character is a single slot and you can't do that in this case. Use cell arrays instead:

Year_Category = cell(ny,1); %// Change
for i = 1:ny;
    if (xy(i)< Lower_Limit)
        Year_Category{i} = 'Dry'; %// Change
    elseif (xy(i)> Upper_Limit)
        Year_Category{i} = 'Wet'; %// Change
    else
        Year_Category{i} = 'Average'; %// Change
    end
end
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you rayryeng; it worked but it give me answer as a cell. But I would like to have the results as an array (double). When i have tried to convert cell into double (Year_Category_M = str2double(Year_Category); ) it shows all the values as NaN.
That's not possible. You are trying to assign characters to a double array. That's why I suggested a cell instead. This can work if you change Dry, Wet and Average to actual numbers instead... like 0, 1, or 2. You can't do this using characters.

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.