Effacer les filtres
Effacer les filtres

Info

Cette question est clôturée. Rouvrir pour modifier ou répondre.

Subscripted assignment dimension mismatch using text value

2 vues (au cours des 30 derniers jours)
Jay
Jay le 18 Oct 2014
Clôturé : MATLAB Answer Bot le 20 Août 2021
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?

Réponses (2)

Image Analyst
Image Analyst le 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 le 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

Cette question est clôturée.

Community Treasure Hunt

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

Start Hunting!

Translated by