Jump to content

One more matlab problem


zhupolongjoe

Recommended Posts

I know you say I should ask on a matlab community, but I've tried and you guys are way more useful.

 

 

I need a code that takes a line, which contains some words in it, all words are separated from each other with a space, but there is no other punctuation. The function should give the length of the words...i.e.:

 

lengths('apple grape me') should give 5 5 2

 

Here is what I have tried, but it does not work...it returns "0"

 

function legnthwords=lengths(line)

spaces=findstr(' ', line);

index=-1;

while index~=spaces;

index==index+1

end

end

Link to comment
https://forums.phpfreaks.com/topic/146408-one-more-matlab-problem/
Share on other sites

As before, PHP function to perform this task:

function lengthWords($line) {
$words = explode(' ',$line);
$wordLengths = array();
foreach($words as $word) {
	$wordLengths = strlen($word);
}
return $wordLengths;
}

print_r(lengthWords('apple grape me'));

function [ lengths ] = lengthwords( line )

count = 0;
lengths = [];
line = [line, ' ']

for i = 1 : length(line)
    if line(i) ~= ' '
        count = count + 1
    else
        lengths(end+1) = count
        count = 0;
    end
end

end

 

I hope this isn't homework :P

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.