达到固定值时将向量转换为矩阵 [英] Turn vector into matrix when a fixed value is reached

查看:68
本文介绍了达到固定值时将向量转换为矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题是对此问题的补充:链接到原始问题 我该如何在代码中实现,所有行都应填充"为0,例如第6列.

This question is an addition to this one: Link to original question How can I implement in the code, that all rows should be "filled up" with 0 let's say up to column 6.

这是它应如何工作的示例.

This is an example of how it should work.

V [18x1]: [6000, 6500, 5000, 8000, 15000, 15500, 16000, 6000, 4000, 16500, 14000, 400, 5000, 6000, 9000, 12000, 13000, 5000]

Matrix [3x4]: 
1.row [8000 15000 15500 16000 0 0] 
2.row [16500 14000 0 0 0 0] 
3.row [9000 12000 13000 0 0 0]

推荐答案

您应修改答案,如下所示:

You should modify the answer likes the following:

result = []; new_row = 1; col_num = 1; row_num = 0;
limit = 7000;
for idx = 1:length(V)
    if col_num == 7
        new_row = 1
    end
    if(V(idx) > limit && new_row == 0) % case 1
        result(row_num, col_num) = V(idx);
        col_num = col_num + 1;
    elseif(V(idx) > limit && new_row == 1) %case 2
        row_num = row_num + 1; new_row = 0; col_num = 2;
        result(row_num, 1) = V(idx);
    elseif(V(idx) <= limit) %case 3
        new_row = 1;
    end
end
if size(result,2) < 6
    result(1,6) = 0;
end

添加以下行以检查col_num不超过6:

Add the follwoing lines to check the col_num does not exceed from 6:

if col_num == 7
    new_row = 1
end

最后,检查result的列大小是否不是6,修改result矩阵,如下所示:

At the end, check if the column size of the result is not 6, modify the result matrix likes the following:

if size(result,2) < 6
    result(1,6) = 0;
end

这篇关于达到固定值时将向量转换为矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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