In: Computer Science
Q22 Variable s is defined as a string vector. We display the first a couple of elements: s = [ ′′height′′ , “12.5” , “weight ′′ , ′′103-9′′ , ′′NWD′′, ... ].
Code to do the following.
I need help to solve this in MATLAB programming please.....
s = [ "height" , "12.5" , "weight" , "103-9" ,
"NWD","height"];
%replace all elements "height” with “dimension” for s. Solve this
using loop
for i=1:length(s)
if s(i)=="height"
s(i)="dimension";
end
end
s
s = [ "height" , "12.5" , "weight" , "103-9" ,
"NWD","height"];
%replace all elements "height” with “dimension” for s. Solve this
without using loop
s(s=="height")="dimension"
s = [ "height" , "12.5" , "weight" , "103-9" ,
"NWD","height"];
%create a new character vector s1, which concatenate all elements
of s horizontally. That is, s1 = ′′height12.5weight
103-9NWD...".
s1='';
for i=s
s1=[s1,char(i)];
end
s1