在另一个列中添加一个具有某种条件的列,如matlab中的excel的sumif [英] addition of one column with certain condition in another colum, like sumifs of excel in matlab

查看:740
本文介绍了在另一个列中添加一个具有某种条件的列,如matlab中的excel的sumif的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

在另一个列中添加具有特定条件的一列,如excel的sumif


这里我想计算第2列的数字,在第1列中有相同的条件,
im在这里再次指示,



A = [1 2; 2 3; 3 4; 3 2; 4 3; 5 4; 5 2; 6 3; 7 2; 8 3] p>

现在我想在列2中计数2,3和4的数,条件是当列1的范围从0到3,3到6,6到9,9到12这样。



回答是这样的



范围no2,no3,no4



0到3-- 2 ----- 1 ------- 1



3到6 --- 1 ------ 2 ------- 1



6至9-- --1 ----- 1 ----- -0



帮助我,,,,等待您的回复..

解决方案

您可以这样做:

  for(n = 1:size(A,1))
if(A(n,1)> = 0&& A(n,1) 3)
%做某事
elseif()%下一个测试条件
%执行某些操作。
end
end

另一种选择是使用for循环



更新如下:



pre> for(n = 1:size(A,1))
switch(mod(A(n,1),3))
将在您的列中为0,1,2
%code
case 1
etc ...
end
end

在matlab中的模数信息



在matlab中切换信息


Possible Duplicate:
addition of one column with certain condition in another colum, like sumifs of excel

here i want to count the no of 2nd column, with the same conditions in column 1, i m dictating here again,

A=[1 2;2 3;3 4;3 2;4 3;5 4;5 2;6 3;7 2;8 3]

now i want to count no of 2, 3 and 4 in column2, condition is that when column 1 ranges from 0 to 3, 3 to 6, 6 to 9, 9 to 12 like that.

answer is like that

ranges no2, no3, no4

0 to 3-- 2-----1-------1

3 to 6---1------2-------1

6 to 9-- --1-----1------0

help me,,,, waiting for your reply..

解决方案

You could potentially do something like this:

for(n=1:size(A,1))
    if(A(n,1) >= 0 && A(n,1) < 3)
        % Do something
    elseif( ) % next test condition
        % Do something there.
    end 
end

Another option would be to use the for loop to go through your first column and using a switch statement to pull the appropriate action based on that input.

Update below:

for(n = 1:size(A,1))
    switch(mod(A(n,1),3))
        case 0 % this will be 0, 1, 2 in your column
            % code
        case 1
            etc...
    end
end

Info on modulus in matlab

Info on switch in matlab

这篇关于在另一个列中添加一个具有某种条件的列,如matlab中的excel的sumif的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆