0

I am trying to figure out a way to make a for loop in which I can compare two cells that will give me two different means. One for class char and the other for class double.

This is what I have so far.

V = {2; 'tree'; 3; 'hope'};
W = {2; 'tree'; 3; 'hope'};


for i = 1:length(V);
    if isequal(class(V{i}), 'double')
        num = V{i}
     elseif isequal(class(V{i}), 'char')
        str = V{i}
    end
end
for i = 1:length(W);
    if isequal(class(W{i}), 'double')
        acc_n(i) = isequal(V{i}, W{i})
    elseif isequal(class(W{i}), 'char')
        acc_s(i) = strcmp(V{i}, W{i})
    end
end
mean_d = mean(acc_n)
mean_s = mean(acc_s)

The output I get is:

acc_n =
     1     0     1
acc_s =
     0     1     0     1
mean_d =
    0.6667
mean_s =
    0.5000 

The output I want is:

1 1 for string, mean = 1. 1 1 for double, mean = 1

How can I do a loop where it only takes the numbers of the cell and the words of the cell separately?

Is there any possible way to only loop through the words or the numbers?

Adriaan
  • 17,741
  • 7
  • 42
  • 75

1 Answers1

0

You can first extract strings and doubles and treat them separately, that will avoid loops.

V = {2; 'tree'; 3; 'hope'};
W = {2; 'tree'; 3; 'hope'};

VChar=V(cellfun(@ischar,V));
WChar=W(cellfun(@ischar,W));

acc_s=VChar==WChar;

VNum=cell2mat(V(cellfun(@isnumeric,V)));
WNum=cell2mat(W(cellfun(@isnumeric,W)));

acc_n=VNum==WNum;

Loop version: I haven't tested this but it should work.

%Assumes V and W have equal number of elements.

acc_n=[];
acc_s=[];
for i=1:numel(V)
    if isequal(class(V{i}), 'double') && isequal(V{i},W{i})
        acc_n=[acc_n true];
    elseif isequal(class(V{i}), 'char') && strcmp(V{i},W{i})
        acc_s=[acc_s true];
    end
end
Autonomous
  • 8,935
  • 1
  • 38
  • 77
  • Is there an alternative way to do this without using cellfun? – user2924450 Nov 01 '15 at 22:52
  • Also, How about using a loop to do this? – user2924450 Nov 01 '15 at 22:54
  • @user2924450 I think a loop would be fine as `cellfun` sometimes is worse than loops. See [here](http://stackoverflow.com/questions/18284027/cellfun-versus-simple-matlab-loop-performance). See my edit. – Autonomous Nov 01 '15 at 22:56
  • The code you gave me for loop has undefined variables like acc_n and acc_s. Also I get whats going on but why do we need to add: numCount=numCount+1; acc_n(numCount)=true; <-- what is this doing? – user2924450 Nov 01 '15 at 23:45
  • See my edit. The `numCount` and `charCount` was counting how many char and double variables have been added as till now, but that's only useful when you have pre-allocated the array. I changed the code now. – Autonomous Nov 02 '15 at 00:57
  • The problem is that this is a little complicated to read and understand. I just want a loop that individually loops through the string elements and the number elements in a cell array. – user2924450 Nov 02 '15 at 00:58
  • I can't make it simpler than this. The problem with looping through strings and numbers separately is that, you will first have to extract them. That's what I did in my first solution. Then you said, you didn't want cellfun. If you really want to do that, then count the numbers and chars by using another for loop, and then perform equality comparsons which would be unnecessary. – Autonomous Nov 02 '15 at 01:07
  • Thank You for your help anyway. this was helpful – user2924450 Nov 02 '15 at 01:29