Error:Expression or statement is incorrect--possibly unbalanced (, {, or [ [closed] - matlab

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I am facing problem in
fw=D_w((IND(k,1),IND(k,2));
and W2(k)= ((D_w(IND(k,1),IND(k,2))/D(IND(k,1),IND(k,2))-1)*10)
There is a problem in the watermark extraction process and there is error in the last of the code . Any suggestions .kindly make some modifications in it .
So, we have to protect it from leaking so, they don’t reach to wrong people .We have to find the person responsible for the leakage of data . if the data distributed to the third party found in the public domain ,it might be very serious threat to the owner of the company.
I=imread('girl512.bmp');
subplot(2,3,1),imshow(I,[]),title('Original Image');
wmsz=1000;
I=I(:,:,1);
[r,c]=size(I);
D=dct2(I);
D_vec=reshape(D,1,r*c);
[D_vec_srt,Idx]=sort(abs(D_vec),'descend');
W=randn(1,wmsz);
subplot(2,3,2),plot(W),title('Watermark');
Idx2=Idx(2:wmsz+1);%choosing 1000 biggest values other than the DC
%finding associated row-column order for vector values
IND=zeros(wmsz,2);
for k=1:wmsz
x=floor(Idx2(k)/r)+1;%associated column in the image
y=mod(Idx2(k),r);%associated row in the image
IND(k,1)=y;
IND(k,2)=x;
end
D_w=D;
%WATERMARK EMBEDDING
for k=1:wmsz
fw=D_w((IND(k,1),IND(k,2));
fw=fw+0.1*fw.*W(k);
end
I2=idct2(D_w);%inverse DCT to produce the watermarked asset
I2_int=uint8(I2);
imwrite(I2_int,'I2_watermarkedn.bmp','bmp');
subplot(2,3,3),imshow('I2_watermarkedn.bmp'),title('Watermarked Image');
%WATERMARK EXTRACTION
W2=[];
for k=1:wmsz
W2(k)=((D_w(IND(k,1),IND(k,2))/D(IND(k,1),IND(k,2))-1)*10);
end
subplot(2,3,4),plot(W2),title('Extracted Watermark');

Lets count them:
12 3 4 opens
fw=D_w((IND(k,1),IND(k,2));
1 23 closes
12 3 2 3 21 cumulative
So, yes, they are unbalanced. You need to fix that. I suspect it can be fixed simply by removing one of the two initial ( characters.
12 3 4 5 6 7 8 opens
W2(k)= ((D_w(IND(k,1),IND(k,2))/D(IND(k,1),IND(k,2))-1)*10)
1 23 4 56 7 8 closes
12 3 4 3 4 32 3 4 3 4 32 1 0 cumulative
Those ones aren't unbalanced, in terms of quantity. I can't vouch for the placement since it's rather complex :-)

Related

Labelling the TS trends using paste and substr

I am trying to label the projected years from 2016 to 2045 in 3 decades i.e. (2016-25, 2026-35, 2036-45)
This is what I have tried:
Projperiod <- paste(201:203, 6, “-“, substr(201:203,3,3),5,sep=“”)
The result is
2016-15 2026-25 2036-35
The last two digits do not appear correctly. This labeling is important as it defines the projection period in next steps.
Any help will be highly appreciated.

All possible combinations of the elements/vectors/list of vectors (Cartesian product)

I have several vectors or lists of vectors and would like to make up all possible concatenations of their entries. Here is an example:
a1=4;
a2=[1,6;1,9;6,9];
a3=[2;7];
That all should result in:
[4,1,6,2]
[4,1,6,7]
[4,1,9,2]
[4,1,9,7]
[4,6,9,2]
[4,6,9,7]
I think my question is similar to this one: Generate all possible combinations of the elements of some vectors (Cartesian product) but I am not really able to adapt the answers to my problem.
EDIT:
Thank you again for the answer and corrections! As beaker already said its works like a charm in octave. Now I would also like to have it a little more flexible for an arbitrary number of a's combined in a cell array (or any other structure which would fit the potential solution better). I made a work around with composing a string and then evaling it. But that does not seem pretty elegant to me. Is it possible to have it more... algorithmic?
I am answering using MATLAB in the hope that the same code works in Octave as well.
Here's a solution based on Amro's answer in the question you linked:
a1=4;
a2=[1,6;1,9;6,9];
a3=[2;7];
nRows = [size(a1,1), size(a2,1), size(a3,1)];
[x,y,z] = ndgrid(1:nRows(1),1:(nRows(2)),1:(nRows(3)));
cartProd = [a1(x(:),:) a2(y(:),:) a3(z(:),:)];
Which results in:
cartProd =
4 1 6 2
4 1 9 2
4 6 9 2
4 1 6 7
4 1 9 7
4 6 9 7
It's a slightly different order than what you showed, but I think it could still be useful to you.

MATLAB: fast creation of vector of indices [duplicate]

This question already has answers here:
Run-length decoding in MATLAB
(4 answers)
Closed 6 years ago.
I have a variable distr=[0 3 1 0 2];, and I have a variable full which should contrain distr(i) times i, for all i.
In this example, i want:
full=[2 2 2 3 5 5];
because distr(2)=3, therefore 3x 2, and so on.
Of course I can do it in a for-loop:
full=zeros([1,sum(distr)]);
cc=1;
for i=1:length(distr)
curr=distr(i);
full(cc:cc+curr-1)=i*ones([1,curr]);
cc=cc+curr;
end
but that is very slow. Do you know of a fast way, using MATLAB's awesome array-oriented style? Thanks!
Not sure, but maybe this will work. I can't check it since I currently don't have MATLAB:
full_tmp = arrayfun(#(i,n) i*ones(1,n),1:length(distr),distr,'uniformoutput',false);
full = cat(2,full_tmp{:});

What is wrong with my simple matlab code

(X^2)(1,2)
X is a square matrix, I just want to get the element from position (1,2) from (X^2) why it does not work?
Indexing is syntactically not allowed in this case. The simplest workaround is to use getfield
X=magic(5)
X =
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
>> getfield(X^2,{1,3})
ans =
725
That's not how Matlab works. You need to assign the result of the matrix multiplication to another matrix, then use it:
A = X^2;
disp(A(1,2));
This is assuming that you really did mean to do matrix multiplication, and not multiply element by element. In the latter case you could have done
disp(X(1,2)^2)
And if you are interested in matrix multiplied result, then
disp(X(1,:)*X(:,2))
will do it, since that is how element (1,2) is calculated. This last solution has the advantage of being very efficient since you only calculate the element you need, instead of computing the entire matrix and throwing N^2-1 elements away just to keep the one. For bigger matrices that will make a difference. Of course it makes the code slightly less readable so I would always recommend writing a comment in your code when you do that - your future self will thank you...
edit take a look at http://www.mathworks.com/matlabcentral/newsreader/view_thread/235798 - that thread broadly agrees with my first statement, although it hints that the syntax you desire might be "part of a future release". But that was said 6 years ago, and it's still not here... It also shows some pretty obscure workarounds; I recommend not going that route (because all the workarounds do is hide the fact that you compute the matrix, then pick just one element. So the workload on the computer is no smaller.)

MATLAB: Step through iterations of a vector

all.
I have a 15 element array = [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15];.
I was wondering if there was a command such that it would step through iterations of the array without repeating itself. In other words, since there is a chance that randperm() will create the same matrix twice, I want to step through each permutation only once and perform a calculation.
I concede that there are factorial(15) permutations, but for my purposes, these two vectors (and similar) are identical and don't need to be counted twice:
[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15]
[15 14 13 12 11 10 9 8 7 6 5 4 3 2 1]
Thus, is there any way to step through this?
Thanks.
I think what you are looking for is perms. randperm returns a single random permutation, you want all the permutations.
So use
my_permuations = perms([1:15]);
If forward-backward is the same as backward-foward then you can use the top half of the list only...
my_permutation_to_use = my_permutations(1:length(my_permutations)/2, :);
You may compare all permutations, but this would require to store all past permutations. Instead a local decision is better. I recommend this simple rule:
A permutation is valid, if the first element is smaller than the last element.
A permutation is redundant, if the first element is larger than the last element.
For small sizes, this could simply be done with this code:
%generate all permutations
x=perms(1:10)
%select only the valid lines, remove all redundant lines
x(x(:,1)<x(:,end),:)
Remains the problem, that generating x for 1:15 breaks all memory limits and would require about 100h.