How to compare each content of string which is of type double (matrix) in if loop recursively?

조회 수: 1 (최근 30일)
Hello Friends,
I have the following code:
P = [1 , 0, 2];
AB = [1, 2, 3];
CD = [4, 5, 6];
EF = [7, 8, 9];
M = {[AB], [CD], [EF]};
for i=1:length(M)
P1 = P(M{i}~=0);
t = M{i};
M2 = t(M{i}~=0);
if ~any(strcmp(M, AB))
f = f(AB);
elseif ~any(strcmp(M, CD))
f = f(CD);
elseif ~any(strcmp(M, EF))
f = f(EF);
end
end
I am trying to run for loop for each i . The problem is if loop. The loop iterates only the 1st if statement for AB; it does not go to elseif statement for CD and EF. I realize the problem could be the way I am using strcmp, but then, I do not know how to do it. Here all matrices/functions are taken just for illustration purpose. They could be anything.
I will appreciate any advice!
%% Below I have given the modified code after Guillaume's suggestion. Please see code below for actual problem.
  댓글 수: 3
Stephen23
Stephen23 2016년 5월 18일
편집: Stephen23 2016년 5월 18일
@hello_world: it would be much easier if you also told us what the expected output should be for those example matrices. Showing us broken code does not explain what you are trying to achieve.
hello_world
hello_world 2016년 5월 18일
ok, I have given an expected output for this problem, though it is for illustration purpose only. I could use any arbitrary function.

댓글을 달려면 로그인하십시오.

채택된 답변

Stephen23
Stephen23 2016년 5월 18일
편집: Stephen23 2016년 5월 18일
You are making this far too complicated. Trying to detect which matrix is being processed in each loop iteration is totally unnecessary: by using a loop this is already specified! Basically you are tying to do everything twice, and this is causing lots of confusion. Try this:
P = [1, 0, 2];
AB = [1, 2, 3];
CD = [4, 5, 6];
EF = [7, 8, 9];
%
fun = @(a,b)sum(plus(a,b)); % define any function here...
%
C = {AB, CD, EF};
out = cell(size(C));
%
for k = 1:numel(C)
mat = C{k};
idx = mat~=0 & ~isnan(mat); % define your index conditions here
out{k} = fun(P(idx),mat(idx));
end
which defines this out cell array:
>> out{:}
ans =
9
ans =
18
ans =
27

추가 답변 (1개)

Guillaume
Guillaume 2016년 5월 18일
There are no strings in your code at all (You create a string literal by enclosing the text in quotes: var = 'thisisastring'). Your M is a cell array containing matrices. I'm actually surprised that strcmp is not giving you an error since you're not comparing strings.
Also, it's not clear what you're trying to do in the loop. Since nothing in the loop depends on the index, you're always going to get the same result.
To find if matrix AB is found in cell array M:
any(cellfun(@(m) isequal(m, AB), M))
  댓글 수: 3
Guillaume
Guillaume 2016년 5월 18일
편집: Guillaume 2016년 5월 18일
@(m) isequal(m, AB)
is an anonymous function for which m is an input. The above is equivalent to
function out = fun(m)
out = isequal(m, AB);
end
In the cellfun call, m will receive in turn the elements of M.
What I've given you is a working example. If only you'd tried it.
AB = [1, 2, 3];
CD = [4, 5, 6];
EF = [7, 8, 9];
M = {[AB], [CD], [EF]};
any(cellfun(@(m) isequal(m, AB), M)) %returns true
any(cellfun(@(m) isequal(m, [1 1 1]), M)) %returns false
hello_world
hello_world 2016년 5월 18일
편집: hello_world 2016년 5월 18일
Thanks. The problem of using cell array is solved. However, for loop still loops over the first if statement; it does not go to the elseif statement. I have written modified code with your improvements above.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by