Please help me fix the count for the nested for-loop

function [ result ] = Clockregister01( R,count )
clc
for j = 1:count
for i = 1: size(RN,1)-1
% RN(i,1:size(RN,2)) = addHexNumber(RN(i,1:size(RN,2)),RN(i+1,1:size(RN,2)))
RN(i) = addHexNumber(RN(i),RN(i+1))
end
end
result = RN;
end

 채택된 답변

Mark Sherstan
Mark Sherstan 2018년 11월 30일
I dont fully understand the end goal of your function but your error was coming from redefining Rn as well as some of your indices. The following now runs however I am not sure what values you need to store / reference but hopefully this will push you in the right direction.
function [result] = Clockregister01(R,count)
R = ['000000000000000000000000000000000000000000';
;'000000007FE906A41162B1720C281817AA5644BC80'
;'FFFFFFFFFFFFFFFFE6604FFCF6DA1466C024D53905'
;'0000000000000000023FEF553317DFDBA061CE3DF3'
;'00000000000000000000000523CE74ABBF21076BB8'
;'FFFFFFFFFFFFFFFFFFFFFFFFFFE04F0941AF9FF0D3'];
RN = R;
for j = 1:count
for i = 1:size(RN,1)-1
% RN(i,1:size(RN,2)) = addHexNumber(RN(i,1:size(RN,2)),RN(i+1,1:size(RN,2)))
A = addHexNumber(RN(i,:),RN(i+1,:));
end
end
result = RN;
end

댓글 수: 11

I'm stil getting this error:
Not enough input arguments.
Error in Clockregister01 (line 11)
for j = 1:count
My question didn't get posted completely, I really want to add-up the hex strings and display the new result, and keep doing the same thing every time I hit the Run button,
The j outer for-loop will keep add-up the new result from the inner for-loop. It seems like I still have an issue with count... any idea??...... Thanks for the help in advance.
Also, I’m trying to set an Upper/lower limit for my count, and if statement shows an error if I exceed the limit.
So you will always have 6 hex strings and they will just get added together?
what are the limits on the count function?
Irwin2020
Irwin2020 2018년 11월 30일
편집: Irwin2020 2018년 11월 30일
This function will work as an accumulator register , not always ill have 6 hex strings maybe more or less. I have tried this function with on for-loop and got the result for one iteration with clock .... Ex:
fprintf('Register state after %d clock \n', counter);
Ex:
Register state after 1 clock
FFFFFFFFFFF333718DDDCCEE13318D
1010101010101010101010144484CACB757795424D
111111FEBD74362BDE79EB9987101D6D
1FFFFFDCAC63251AAAAA01010101FF3212A
EEEEEEEFFFFFFFFFFFFFFFFFF3247D
and keep displaying the new result like:
Register state after 2 clock .... this will have new result as well.
and
Register state after 5 clock ........ till Register state after 10000 clock and so on so forth...
The count limit would be 20000
newState = Currentresult+Previusresult
You must you cell arrays as your length will always be changing. I also added the upper count limit to your code and the displaying of the results. The code runs fine on my machine but has some warnings in your addHexNumber function specifically the dec2hex function. They are just warnings not errors so I will leave it to you to ensure your results are correct.
function [result] = Clockregister01(R,count)
if count > 20000
error('Decrease count below 20000')
end
R = {'000000000000000000000000000000000000000000';
;'000000007FE906A41162B1720C281817AA5644BC80'
;'FFFFFFFFFFFFFFFFE6604FFCF6DA1466C024D53905'
;'0000000000000000023FEF553317DFDBA061CE3DF3'
;'00000000000000000000000523CE74ABBF21076BB8'
;'FFFFFFFFFFFFFFFFFFFFFFFFFFE04F0941AF9FF0D3'};
RN = R;
for j = 1:count
for i = 1:size(RN,1)-1
RN{i} = addHexNumber(RN{i},RN{i+1});
end
fprintf('Register state after %d clock \n',j);
RN
end
result = RN;
end
I dont know where is the problem, but I 'm still getting an error message from :
>> Clockregister
Not enough input arguments.
Error in Clockregister (line 5)
if count > 20000
Any idea why I’m getting this ? I'm running latest version of Matlab..
How are you running your code? Type this into the MATLAB command line:
clear all
close all
clc
Clockregister01(0,100);
It appears you are not calling the right function and you have no input arguments.
Irwin2020
Irwin2020 2018년 11월 30일
편집: Irwin2020 2018년 12월 3일
Your approach work just fine but it didn’t reach my goal, the result I’m getting is far from the correct result, when I add the hex string line by line (by hand) I get the correct result....
your code display only the first result and its correct, as I mentioned above I have tried my first inner for-loop and did the job
Clockregister(0,1)
Register state after 1 clock
ans =
6×1 cell array
{["000000007FE906A41162B1720C281817AA5644BC80" ]}
{["101010101010101017FE9016A4018D30117F0130133D8E7B8B2AF585" ]}
{["101010101010101010101010101010101F9A013F523AF2F4427196A476F8" ]}
{["1111111111111111113401F016B67E6548760193D6A9AB" ]}
{["10101010101010101010101010101010101010101010101523BFC4B5011D1A76C8B"]}
{'FFFFFFFFFFFFFFFFFFFFFFFFFFE04F0941AF9FF0D3'}
But after increasing the count Clockregister(0,2) or Clockregister(0,3), I get completley different answer!..
Try it yourself and let me know please.
I took this result and did it by hand and this is what I got :
addHexNumber('000000007FE906A41162B1720C281817AA5644BC80','101010101010101017FE9016A4018D30117F0130133D8E7B8B2AF585')
ans =
"101010108FF916B429614188A0129A547BBD545EC93"
>> addHexNumber('101010101010101017FE9016A4018D30117F0130133D8E7B8B2AF585','101010101010101010101010101010101F9A013F523AF2F4427196A476F8')
ans =
"20202020202020202701EA026B4119D403119026F6578816FCD9C8C29"
And I tried to add the 3rd to the 4th this is what I got: Index exceeds array bounds.
>> addHexNumber('101010101010101010101010101010101F9A013F523AF2F4427196A476F8','1111111111111111113401F016B67E6548760193D6A9AB')
Index exceeds array bounds.
Error in addHexNumber (line 11)
num2 =typecast(uint64(hex2dec(hex2(i))), 'int64');
Your first array (hex1) is 60 digits long, your second array is 46 digits long (hex2). In your function you use the length of hex1 to iterate over both arrays.
size = length(hex1);
for i = size:-1:1 %The index variable i starts at size, then decreases in steps of 1 until it reaches 1.
h1 =typecast(uint64(hex2dec(hex1(i))), 'int64');
h2 =typecast(uint64(hex2dec(hex2(i))), 'int64');
%...
Any idea on how to iterate over both arrays?

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

추가 답변 (1개)

Dennis
Dennis 2018년 12월 5일
Your addHexNumber function actually does not work correctly. I think you need to change
if bothSum > 16
to
if bothSum > 15 % or >=16

댓글 수: 1

I believe that too, I have debugged and modified it to > 15 and >=16 still doesn’t work, any other suggestions?

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

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

질문:

2018년 11월 30일

편집:

2018년 12월 26일

Community Treasure Hunt

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

Start Hunting!

Translated by