Info

This question is closed. Reopen it to edit or answer.

Subscripted assignment dimension mismatch using text value

1 view (last 30 days)
I have a vector u(1,1) = 6 and a vector delta_hat = [1,2,3,4,5,6]'
I have tried to create a function so that when the value is even it will populate the existing element with 'X_naught' and if odd populate 'Y_naught':
for i = 1:u(1,1)
if mod(delta_hat(i,1),2) == 0
delta_hat(i,1) = 'X_naught'
%number is even
else
delta_hat(i,1) = 'Y_naught'
%number is odd
end
end
Why am I getting the above error?

Answers (2)

Image Analyst
Image Analyst on 18 Oct 2014
You need to use a cell array instead of a regular numerical array because you'll be mixing numbers and strings. Read about cell arrays here:

Star Strider
Star Strider on 18 Oct 2014
Your original ‘delta_hat’ is defined as a (1x6) double vector. You need to define a new array (I called it ‘delta_str’ here), and adding a dimension to ‘delta_str’ to accommodate the length.
With those few modifications, it works:
u(1,1) = 6;
delta_hat = [1,2,3,4,5,6];
for i = 1:u(1,1)
if mod(delta_hat(i),2) == 0
delta_str(i,:) = 'X_naught';
%number is even
else
delta_str(i,:) = 'Y_naught';
%number is odd
end
end

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!