How to run a loop in matlab even a struct condition satisfies and warning message? - matlab

Lets say I have a struct with three 1x1 struct elements with 5 fields in them respectively, the struct is GroupA and 1x1 struct elements are A,B,C and each has 5 fields in them say ID, E, F, G and h. I Need to check for each of them if h is same in any both and give a warning saying h is same in A & B. For example:
struct GroupA
A B C
Id ID ID
E E E
F F F
G G G
h h h
I wrote
for B_card=1:size(GroupA,2)-1
for C_card=(B_card+1):size(GroupA,2)
if strcmp(GroupA(B_card).h,GroupA(C_card).h)==1
warning('The h is same in',GroupA(B_card).ID,'&',GroupA(C_card).ID);
end
end
end
I got two problems: one the loop end (not sure whether is ending or not, can't understand) when an if condition satisfies and the warning message is only showing " The h is same in". I am quite new to Matlab so explained as good as I can, please let me know if you need some more explanation and thanks for your help.

for B_card=1:size(GroupA,2)-1
for C_card=(B_card+1):size(GroupA,2)
if strcmp(GroupA(B_card).h,GroupA(C_card).h)==1
warning('The h is same in',num2str(GroupA(B_card).ID),'&',num2str(GroupA(C_card).ID));
end
end
end
its working like this also & thanks every one

Related

MATLAB Simple Calculation

I am working on MATLAB on my own, and was doing problem 9 on Project Euler
It states
" A Pythagorean triplet is a set of three natural numbers, a < b < c, for which,
a2 + b2 = c2
For example, 32 + 42 = 9 + 16 = 25 = 52.
There exists exactly one Pythagorean triplet for which a + b + c = 1000.
Find the product abc."
Below is the code I wrote; however, it compiles, but does not produce and output. I was hoping to get some feedback on what's wrong, so I can fix it.
Thanks,
syms a;
syms b;
syms c;
d= 1000;
d= a + b + c ;
ab= a.^2 + b.^2;
ab= c.^2;
c
I propose a vectorized way (that is, without using loops) to solve the problem. It may seem relatively complicated, especially if you come from other programming languages; but for Matlab you should get used to this way of approaching problems.
Ingredients:
Vectorization;
Indexing;
Transpose;
Implicit singleton expansion;
hypot;
find.
Read up on these concepts if you are not familiar with them, and then try to solve the problem yourself (which of course is the whole point of Project Euler). As a hint, the code below proceeds along these lines:
Generate a 1×1000 vector containing all possible values for a and b.
Compute a 1000×1000 matrix with the values of c corresponding to each pair a, b
From that compute a new matrix such that each entry contains a+b+c
Find the row and column indices where that matrix equals 1000. Those indices are the desired a and b (why?).
You'll get more than one solution (why?). Pick one.
Compute the product of the obtained a and b and the corresponding c.
Once you have tried yourself, you may want to check the code (move the mouse over it):
ab = 1:1000; % step 1
cc = hypot(ab,ab.'); % step 2
sum_abc = ab+ab.'+cc; % step 3
[a, b] = find(sum_abc==1000); % step 4
a = a(1); b = b(1); % step 5
prod_abc = a*b*cc(a,b); % step 6

Separating some part of a matrix in matlab

I have a 2m by 2 matrix called A and a 2 by m matrix called B. Let's name the product of A by B, C:
C= A*B;
which C is a 2m by m matrix. I want to find matrix F which contains some parts of C. F is a m by 2 matrix and contains elements C(1,1), C(2,1), C(3,2),C(4,2),C(5,3), C(6,3),...C(2m-1,m), C(2m,m).
For example, consider
A = [0,2;1,3;4,7;8,3;4,5;1,2]
B=[1,4,6;5,7,3]
C=A*B;
In this case:
F=[C(1,1),C(2,1);C(3,2),C(4,2);C(5,3),C(6,3)]
But I like to find F without calculating all elements of C. Because I think calculating all elements of C would be time wasting for large values of m. Could anyone suggest a way to find F in general case?
Using Indexing:
F = [C(1:2*m+2:end);C(2:2*m+2:end)]'
To find F without calculating C you can instead use:
F=cell2mat(arrayfun(#(x) A(2*x-1:2*x,:)*B(:,x), 1:m,'uniformoutput',0))'
(You must set m, A, and B as defined in the question)
Explanation:
Each row of F is the transpose of the product of a submatrix in A and a column of B. For example, the first row in F is the transpose of:
A(1:2,:)*B(:,1)
The next row is the transpose of:
A(3:4,:)*B(:,2)
etc.
So this method only calculates the necessary values, by multiplying each column of B only by the corresponding submatrix of A and avoids calculating the unused values in C.

Get values for 2 arguments from 1 equation

I have this equation: p = (1 + (q-1)*B*T) ^ (-1/q-1)
The values p, T are known and the diagram p - T makes curbe. I want to calculate q and B so that curbe be as close to straight line is posible.
Some values are:
T p
1 0,999147061
2 0,997121331
3 0,994562513
Is there any way to make matlab (or sth else) to give me the values of B and q ?

Efficient way of mapping similar inputs to similar outputs

Is there a efficient way of approaching this particular problem in matlab.
I am trying to map this matrix or possible array BeansRice (see below)
Beans={0:1,0:1,0:2,0:2,0:2,0:2,0:1,0:1,0:2,0:2}
[a b c d e f g h i j ] = ndgrid(Beans{:})
BeansRice = [a(:) b(:) c(:) d(:) e(:) f(:) g(:) h(:) i(:) j(:)]
into a matrix/array BR (see below)
BR=[abc, de, fg, hij];
where if columns a, b and c each have values 0 (ties preference), I have preference for c>b>a. If all columns a, b and c each have values 1 (ties no preference), BR(1)=1. If columns a and b have values 0 and column c has value 2, BR(1)=2. If columns a and b have values 1 and column c has value 2, BR(1)=1.
I have an if function with indexing but I was thinking if it is possible to improve it, using the rank/order of the values in the matrix to break ties. Looking for a more efficient process as this is only a sub of a large problem.
You can use logical indexing instead of if conditions. For example
BR1(a==1 & b==1 & c==1)=1
BR1(a==0 & b==0 & c==2)=2
BR1(a==1 & b==1 & c==2)=1
...
then process the other parts, BR2(d==... & e>...)=##, then concatenate to obtain what you need
BR=[BR1(:) BR2(:) ...]
etc...

Matrix patterns

I have two 9x9 matrices, A and B.
I would like to create a large matrix C with the following pattern
A B B B B B
B A B B B B
B B A B B B
B B B A B B
B B B B A B
B B B B B A
As you can see, the A matrices are on the diagonal, the B are everywhere else. I'm trying to create a code so that this pattern continues no matter how great the dimensions are.
E.g. 10 matrices x 10 matrices still has matrix A along the diagonal and B's everywhere else.
Best to use horzcat and vertcat or something else like blkdiag? I'd rather not convert these matrices to cells as matrix A and B already contain information.
Thank you everyone for taking the time to read.
How about (refined)
maskcell = repmat( {ones(size(A))}, 1, 10 );
maskdiag = blkdiag( maskcell{:} );
AA = repmat( {A}, 1, 10 );
AD = blkdiag( AA{:} );
BB = repmat( B, 10, 10 );
C = BB .* (maskdiag == 0) + AD
Following on from the entirely valid comments below, I added the 'mask' to ensure that the correct pieces are selected from C.
C=B(~eye(size(B)))+A(eye(size(A))) should get you what you want. Could be a faster way to combine the use of eye though...
kron(eye(10), A) + kron(~eye(10), B)