insert string in a numerical vector

Hello there,
I have a value vector (1-by-n) and I am trying to replace the number in a given element by a string. How can I achieve this?
my code is:
n=length(A);
k=100;
for i=1:n
if A(i)<k
A(i)= 'wrong';
end
end

Answers (3)

You need to do a cell array to do that:
A = {200, 99, 300}
n=length(A);
k=100;
for i=1:n
if A{i} < k
A{i} = 'wrong';
end
end
celldisp(A)

10 Comments

I have tried both ways but none of them works. Here is my new code:
n=length(Vol); B=num2cell(Vol); C=num2cell(Int); for i=1:n if Vol(i) < VolMin B{i} = 'inactive'; end if Int(i)< IntMin C{i}= 'illiquid'; end end
This is substantially different than the code you asked us to fix. Initially you just has "A" and now you have 4 arrays (Vol, Int, B, & C). In your new code, what is Vol, Int, VolMin, and IntMin?
You can't do what you said UNLESS you use cell arrays so that's why Azzi and I posted virtually the same code within seconds of each other.
I want to create a function which has as 4 inputs (2 vectors 1-by-n (Vol,Int) and two elements VolMin and IntMin (the elements are set by the user). Now I want to check if the values in each vector are less the the value that the user has set and if they are to replace the number contained in the vectors by the appropriate string. Vol and Int are given vectors.
I think this is a bad approach - converting double arrays to cell arrays if an element is invalid. Why don't you just replace the bad values with VolMin or IntMin instead of a string?
Vol(Vol<VolMin) = VolMin; % Or Vol = max(Vol, VolMin);
Or output a vector that says what elements are bad, like
badElements = find(Vol < VolMin);
Either would be preferable to changing the class of the variables from double to cell.
I see your point. I am trying to combine both (initial value and the string) as the vectors contain data I don't want to lose or reform. Can you think of another way?
Thanks
I tried the other ways you typed last, but I get this message: "??? Undefined function or method 'lt' for input arguments of type 'cell'."
That's because I was assuming they were simple numerical arrays, not complicated cell arrays. I really don't recommend you use cell arrays. What is the purpose of writing a string into a location instead of fixing it or replacing it with nan or something?
yes you are right, but can you tell me what do you mean by saying simple numerical arrays and complicated cell arrays?
e.g. K=[13;44;6;42;5;78;37;8;5;89;9] is a simple numerical array no matter its size, correct?
Here's an explanation of cell arrays: http://matlab.wikia.com/wiki/FAQ#What_is_a_cell_array.3F It will let you mix numbers, strings, structures, or any type of variable you want into a single array. But they're a pain to deal with. People are always using parentheses instead of braces and vice versa even though you can use both, you just have to use them at the right time. That's why it's complicated. For your "K", it's not the size - your K is a simple numerical array no matter the size. But if you wanted the 3rd element of K to be "wrong" instead of 6, now it can't be a simple numerical array anymore. It has to be a cell array because that's the only type of array that can mix numbers and strings. Well, okay, you could have an array of structures too, which is simpler than cell arrays to understand, but somewhat different also. Why don't you give me the "big picture"? Why do you want to do what you asked? Why do that (put in strings) instead of fixing the data or flagging it with nan's?
Because I want the observer of the filtered data to be able to see the current value of the i element of the array and the comment which says that this value is 'inactive'. Thanks a lot for your help, it is really precious.

Sign in to comment.

It is better to keep the data and the classifications in different variables. This can be done without loops also, e.g.:
Vol = rand(1, 100);
B = cell(1, 100);
C = cell(1, 100);
B(Vol < 0.3) = {'inactive'};
C(Vol > 0.9) = {'illiquid'};
n=length(A);
k=100;
B=num2cell(A);
for i=1:n
if A(i)<k
B{i}= 'wrong';
end
end

2 Comments

I have tried both ways but none of them works. Here is my new code:
n=length(Vol);
B=num2cell(Vol);
C=num2cell(Int);
for i=1:n
if Vol(i) < VolMin
B{i} = 'inactive';
end
if Int(i)< IntMin
C{i}= 'illiquid';
end
end
@jimaras: Please donot write "does not work", but explain the problem: Do you get an error message or do the results differ from your expectations?

Sign in to comment.

Categories

Asked:

on 2 Feb 2014

Answered:

Jan
on 2 Feb 2014

Community Treasure Hunt

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

Start Hunting!