需要在code转换为Matlab的建议 [英] Need suggestion on Code conversion to Matlab

查看:131
本文介绍了需要在code转换为Matlab的建议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Matlab编程新的,但必须转换在Matlab的C程序。有这使我感到困惑几部分组成。我在这里把零件C和Matlab和寻找您的建议对改善code,因为满code未给予正确的输出:

C code:

  J = 0;
 对于(i = 0; I< 256;我++){
     J =(J + S [I] +键[我的strlen%(键)])256%;
     INT T = S [我]
     S [i] = S [J]。
     S [J] = T;
     }

Matlab的code:

 乐=长度(密钥);
     SC = 0:255;
     输出= 0;
 为I0 = 1:255
     输出= REM((输出+ SC(I0 + 1)+键(REM(I0,乐)+1)),256);
     TM = SC(I0 + 1);
     SC(I0 + 1)= SC(OUTPT + 1);
     SC(OUTPT + 1)= TM;
 结束


解决方案

有关这个C code:

  J = 0;
  对于(i = 0; I< 256;我++)
  {
     J =(J + S [I] +键[我的strlen%(键)])256%;
     INT T = S [我]
     S [i] = S [J]。
     S [J] = T;
  }

我会得到这个Matlab的code:

  J = 0;
对于i = 1:256
    J = MOD第(j + S(ⅰ)+键(模(ⅰ-1,长度(键))+ 1),256);
    T = S(ⅰ);
    序列s(i)= S(J + 1);
    S(J + 1)= T;
结束

于是两个问题:


  1. 在C是既不完全等同于 REM 也不 MOD 在Matlab中,除非所有的数字总是积极在这种情况下也没关系。如果你正在处理负数,那么你需要做一些调查,在其中你之后。

  2. 从0分度环 - > 255用C应该从1去 - > 256在Matlab,因为它在1,而不是从0开始的索引数组类似于C

I am new in Matlab programming but have to convert a C program in Matlab. There are few parts which is making me confused. I am putting here the parts for both C and Matlab and looking for your suggestion for improvement of the code because the full code is not giving right output:

C Code:

     j = 0;
 for (i=0;i<256;i++){
     j = (j+S[i]+key[i%strlen(key)]) %256;
     int t = S[i]; 
     S[i] = S[j]; 
     S[j] = t;
     }

Matlab Code:

     le = length(key);
     sc = 0:255;
     output = 0;
 for i0 = 1:255
     output=rem((output+sc(i0+1)+key(rem(i0,le)+1)),256);
     tm = sc(i0+1);
     sc(i0+1) = sc(outpt+1);
     sc(outpt+1) = tm;
 end

解决方案

For this C code:

  j = 0;
  for (i=0;i<256;i++)
  {
     j = (j+S[i]+key[i%strlen(key)]) %256;
     int t = S[i]; 
     S[i] = S[j]; 
     S[j] = t;
  }

I would get this Matlab code:

j = 0;
for i = 1:256
    j = mod(j + S(i) + key(mod(i-1, length(key)) + 1), 256);
    t = S(i);
    S(i) = S(j+1);
    S(j+1) = t;
end

So two issues:

  1. % in C is neither exactly the same as rem nor mod in Matlab unless all your numbers are always positive in which case it doesn't matter. If you are dealing with negative numbers then you need to do a bit of research into which you're after.
  2. an indexing loop from 0 -> 255 in C should go from 1 -> 256 in Matlab because it begins indexing arrays at 1 rather than 0 like in C.

这篇关于需要在code转换为Matlab的建议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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