So, I have 1x200 matrix, (row vector). I want to find difference (x(0)-x(1), x(0)-x(2)...) between all elements of an array. How do I do that ?
I should be having 200x200/2 values.

 Réponse acceptée

per isakson
per isakson le 17 Fév 2019
Modifié(e) : per isakson le 17 Fév 2019
Hint:
%%
row = rand(1,6);
cell2mat( reshape( arrayfun( @(jj) row(jj)-row, (1:6), 'uni', false ), [],1 ) )
outputs
ans =
0 -0.0790 -0.0644 0.2865 0.0233 0.5075
0.0790 0 0.0146 0.3655 0.1023 0.5866
0.0644 -0.0146 0 0.3509 0.0877 0.5719
-0.2865 -0.3655 -0.3509 0 -0.2633 0.2210
-0.0233 -0.1023 -0.0877 0.2633 0 0.4843
-0.5075 -0.5866 -0.5719 -0.2210 -0.4843 0

4 commentaires

Ana
Ana le 17 Fév 2019
wow thank you! is there a way to differentiate zero values from x(0)-x(0) from the actual 0 difference between two elements?
The x(jj)-x(jj) values are obviously on the main diagonal, but I guess that is of little help.
You could replace the x(jj)-x(jj) values by another value, e.g. NaN
%%
row = rand(1,6);
len = length(row);
dst = cell2mat( reshape( arrayfun( @(jj) row(jj)-row, (1:len), 'uni', false ), [],1 ) );
%%
lix = logical( eye(len) ); % logical index
dst( lix ) = NaN;
then you could do things like
>> dst >= 0.5
ans =
6×6 logical array
0 0 0 1 0 0
0 0 0 0 0 0
0 1 0 1 1 1
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
and
>> dst == 0
Ana
Ana le 18 Fév 2019
Can you explain me exactly what does input into arrayfun mean?
( @(jj) center(jj)-center, (1:233), 'uni', false ), some kind of for loop ?
per isakson
per isakson le 18 Fév 2019
Modifié(e) : per isakson le 18 Fév 2019
Yes, arrayfun performs a loop. Roughly
len = 233;
cac = cell( 1, len );
for jj = 1 : len
cac{jj} = center(jj) - center;
end

Connectez-vous pour commenter.

Plus de réponses (1)

madhan ravi
madhan ravi le 18 Fév 2019
Straightforward:
row.'-row

3 commentaires

Ana
Ana le 18 Fév 2019
I don't understand?
madhan ravi
madhan ravi le 18 Fév 2019
Did you run the code?
per isakson
per isakson le 18 Fév 2019
Modifié(e) : per isakson le 18 Fév 2019
%%
row = rand(1,6);
c1 = cell2mat( reshape( arrayfun( @(jj) row(jj)-row, (1:6), 'uni', false ), [],1 ) );
%%
c2 = reshape(row,[],1) - row; % The name, reshape, communicates the intent
>> c2-c1
ans =
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0

Connectez-vous pour commenter.

Catégories

En savoir plus sur Mathematics dans Centre d'aide et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by