MARIE: My Skipcond 400 doesn't seem to stop the program from barreling through the loop =C What is wrong here, I do not even know? - marie

Input
Store b
Output
Input
Store c
Output
Loop, Load a
Add b
Store a
Load c
Subt i
Store c
Output
Skipcond 400
Jump Loop
Load a
Output
Halt
a, DEC 0
b, DEC 0
c, DEC 0
i, DEC 1
z, DEC 0

If your subt instruction shall subtract one (1) from c, wouldn't you need to use immediate addressing, like #i?

Related

RSA Encryption problem, C, e and N are given

I have the follwing task: the public key(e,N) = (5,299) and the encrypted message C = 60
Now I have to find the original message without knowing the private key d. So I come up with the following equation according to how C is calculated:
60 = M^5 mod 299, where M < n.
But I have no idea how to continue, trying each number from 0 until 298 isn't really a good method. Any help would be appreciated!
You can factorize 299 into 13 and 23, so you have p and q. And from there you can calculate phi(n), which is 12*22=264. Now you need to calculate the inverse of e, or solve the expression e * d = 1 mod 264. As 264 is 265-1 and e is 5 you can simply get to 265 by calculating 5*53. This will be your d. And now you have everything you need, as you can calculate 60^53 mod 299, which is 21, the original message M.
To verify the solution, calculate 21^5, which is 4084101. That mod 299 is 60, your original encrypted message C.

Is there a more efficient to perform this matrix operation in Matlab

I want to make the following thing in Matlab: given a matrix H, I want to build a matrix H* of same size such that H*(:,i) is the sum of the next columns (i.e. i+1 -> n) of H. So for example, if H is
H =
2 4 7 14
3 5 11 -3
I am expecting H* to be
25 21 14 0
13 8 -3 0
So far, I have done the following piece of code but it involves a for loop, so I am not expecting it to be very efficient (especially, my matrix will have a big number of columns in the practical application I will use).
H_tilde=zeros(size(H));
for i=1:size(H,2)
H_tilde(:,i)=sum(H(:,i+1:size(H,2)),2);
end
Is there a way to make it better?
Use cumsum along the 2nd dimension with the 'reverse' option, as follows:
H_tilde = [cumsum(H(:, 2:end), 2, 'reverse') zeros(size(H,1), 1)];

When usng rows with LAPACKE_sgetrs, why must ldb=1 (instead of 3, instead of n)?

We want to solve x for Ax=b,
A=
0 2 3
1 1 -1
0 -1 1
b=
13
0
1
x=
1
2
3
The program below first writes A=P*L*U. It works with columns. It is something like:
float a[3*3]={
0,1,0,
2,1,-1,
3,-1,1
};
float b[8]={
13,
0,
1
lapack_int n=3,lda=3,ldb=3,nrhs=1,info,piv[3];
info= LAPACKE_sgetrf(LAPACK_COL_MAJOR,n,n,a,lda,piv);
info= LAPACKE_sgetrs(LAPACK_COL_MAJOR,'N',n,nrhs,a,lda,piv,b,ldb);
This works. Now I would like to program with rows:
float a[3*3]={
0,2,3,
1,1,-1,
0,-1,1
};
float b[8]={
13,
0,
1
lapack_int n=3,lda=3,ldb=1,nrhs=1,info,piv[3];
info= LAPACKE_sgetrf(LAPACK_ROW_MAJOR,n,n,a,lda,piv);
info= LAPACKE_sgetrs(LAPACK_ROW_MAJOR,'N',n,nrhs,a,lda,piv,b,ldb);
My question is: why must ldb=1 (instead of 3)?
This behavior is due to the wrapper LAPACKE.
If LAPACK_COL_MAJOR is used, the wrapper almost directly calls sgetrs() of LAPACK, as shown in the source of LAPACKE. Hence, the leading dimension ldb of the array b must be equal or higher than the number of rows of the matrix a, that is n=3. Therefore, the requirement is LDB >= max(1,N) as in sgetrs().
One the other hand, if LAPACK_ROW_MAJOR is used, b is transposed. Consequently, the leading dimension of the array ldb is now related to the number of right hand sides nrhs=1. The requirement is now LDB >= max(1,NRHS) as tested on line 59 : if( ldb < nrhs ). Then the array b and the matrix are transposed by calling LAPACKE_sge_trans. Finally, sgetrs() is called using lbd=n and the result is transposed back.

Save to array in for loop, with steps - Matlab

Okay, this is a bit tricky to explain, but I have a long .txt file with data (only one column). It could look like this:
data=[18
32
50
3
19
31
48
2
18
33
51
4]
Now, every fourth value (e.g. 18, 19, 18) represents the same physical quantity, just from different measurements. Now, I want Matlab to take every fourth value and put it into an array X=[18 19 18], and like wise for the other quantities.
My solution so far looks like this:
for i=1:3;
for j=1:4:12;
X(i)=data(j);
end
end
... in this example, because there are three of each quantity (therefore i=1:3), and there are 12 datapoints in total (therefore j=1:4:12, in steps of 4). data is simply the loaded list of datapoints (this works fine, I can test it in command window - e.g. data(2)=32).
My problem, doing this, is, that my array turns out like X=[18 18 18] - i.e. only the last iteration is put into the array
Of course, in the end, I would like to do it for all points; saving the 2nd, 6th, and 10th datapoint into Y and so on. But this is simply having more for-loops I guess.
I hope this question makes sense. I guess it is an easy problem to solve.
Why don't you just do?
>> X = data(1:4:end)
X =
18
19
18
>> Y = data(2:4:end)
Y =
32
31
33
You can reshape the data and then either split it up into different variables or just know that each column is a different variable (I'm now assuming each measurement occurs the same number of times i.e. length(data) is a multiple of 4)
data = reshape(data, 4, []).';
So now if you want
X = data(:,1);
Y = data(:,2);
%// etc...
But also you could just leave it as data all in one variable since calling data(:,1) is hardly more hassle than X.
Now, you should NOT use for-loops for this, but I'm gong to address what's wrong with your loops and how to solve this using loops purely as an explanation of the logic. You have a nested loop:
for i=1:3;
for j=1:4:12;
X(i)=data(j);
end
end
Now what you were hoping was that i and j would each move one iteration forward together. So when i==1 then j==1, when i==2 then j==5 etc but this is not what happens at all. To best understand what's going on I suggest you print out the variables at each iteration:
disp(sprintf('i: \tj:'));
for i=1:3;
for j=1:4:12;
disp(sprintf(' %d\t %d',i,j));
end
end
This prints out
i: j:
1 1
1 5
1 9
2 1
2 5
2 9
3 1
3 5
3 9
What you wanted was
disp(sprintf('i: \tj:'));
for i=1:3;
disp(sprintf(' %d\t %d',i,4*i-3));
end
which outputs:
i: j:
1 1
2 5
3 9
applied to your problem:
%// preallocation!
X = zeros(size(data,1)/4, 1)
for i=1:3
X(i)=data(i*4 - 3);
end
Or alternatively you can keep a separate count of either i or j:
%// preallocation!
X = zeros(size(data,1)/4, 1)
i = 1;
for j=1:4:end;
X(i)=data(j);
i = i+1;
end
Just for completeness your own solution should have read
i = 0;
for j=1:4:12;
i = i+1;
X(i)=data(j);
end
Of course am304's answer is a better way of doing it.

How to select Matrix elements with a filter-matrix

I have 2 martices of the same size. The first contains values and the second only elements of 0 and 1 (like boolean). I now want all elements of my first Matrix stored in an array where the second Matrix has a 1 at the same index.
Maybe an example makes that clear:
Matrix 1:
a b c
d e f
g h i
Matrix 2:
0 1 1
1 0 0
0 0 1
output:
[b c d i]
I think this will work in two steps, but i cant get it to work.
This will need two steps indeed.
%# transpose Matrix 1 because Matlab iterates by row first
matrix_1 = matrix_1';
%# read values (transpose M2 as well)
%# also transpose the result to get a row-vector
output = matrix_1(matrix_2')';
Note that this indexing operation only works if matrix_2 is logical. If it isn't, cast it by writing logical(matrix_2) instead.
If your arrays are a and b, with b the mask array, try
a(find(b))
This won't produce the output in the order in your question. If order is important resort to #Jonas' approach.