Switching the elements of an array
58 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hans123
le 25 Mar 2019
Modifié(e) : madhan ravi
le 25 Mar 2019
hi,
I am plotting data from 2 vectors, the x-axis elements (in a vector called M) needs to be flipped and make the first element 0, the last few elements in M recur like this [1 2 3...500 500 500 500...] and these recurring elements should discarded so it should be [...500 500 500 500 499 498... 3 2 0]
The method I went with is flipping each element by creating a new vector (called N) and using a for loop to switch the order. This however changes the size of my array because it ignores the recurring elements. I want to know how to fix this issue.
My code is
for k=1:max(M)-1
N(max(M)-k,:)=k;
end
N(max(M))=0;
The 600x1 M array, becomes a 500x1 N array
*The array M changes because it nested in another loop so I can't use the line for k=1:500
**and I can't use the line for k=1:length(M)-1, because I get the error - Subscript indices must either be real positive integers or logicals.
(Because max(M) = 500 and the length = 600 and N(-100) is not allowed
2 commentaires
John D'Errico
le 25 Mar 2019
Modifié(e) : John D'Errico
le 25 Mar 2019
Please stop re-posting the same question. You have been given good answers. Use of flip is simple, easy to remember, and efficient.
madhan ravi
le 25 Mar 2019
Modifié(e) : madhan ravi
le 25 Mar 2019
Isn’t it straight forward?:
[M(end:-1:2),0]
I am unfamiliar with that line of code, how does it work?
Also, I get the error -
Error using horzcat
Dimensions of matrices being concatenated are not consistent.
size(M) %? what is the size of M ?? is it a vector?
%or try:
MM = M(end:-1:1);
MM(end) = 0
The vector is reversed until the second element and the last element is set as zero (which is the first element in the original vector). You have the code , try it out!
Illustrate with a short 1 by 10 vector of M and N so that it's clear what your desired result should be.
Thanks for that explanation
Still have the error - Error using horzcat
Dimensions of matrices being concatenated are not consistent
M is a varying size matrix, it is in a nested loop so after each iteration the size changes
Also, I couldn't mention this. The element of N that corresponds to M should be swapped too.
I realized this now after playing with the code. I would really appreciate it if you can tell me what is th best approach? Should I swap the N matrix as well.
In case I didn't word my question properly
N = [A B C ... ZZZ]
M = [1 2 3.. 500 500]
should be swapped to
N = [ZZZ... A B C]
and
M = [500 500 500... 3 2 0]
M vector = [1 2 3 4 5 6 7 8 8 8]
N vector = [10 20 30 40 50 60 70 80 90 100]
Desired Result;
M vector (swapped) = [8 8 8 7 6 5 4 3 2 0]
N vector = [100 90 80 70 60 50 40 30 20 10]
Both vectors should swap
M is a nx1 array, where n is in the range of 500 to 1000
M_Swapped = M(end:-1:1);
M_Swapped(end) = 0
N_Swapped = N(end:-1:1)
This might clear up what I am trying to achieve.
I want that graph to follow the best fit line in blue
If I just swap the x-axis the co-ordinates will get messed up, because the y-co-ordinate points pair up with the wrong x-co-ordinates.
My graph should be the same shape as the best fit line
I have used the below code previously as a temporary ffix, but all that does is flip the axes
%reverse axes
set(gca, 'XDir','reverse')
set(gca, 'XDir','reverse')
xt = get(gca, 'XTick');
xtr = linspace(max(xt), min(xt), numel(xt));
set(gca, 'XTick',xt, 'XTickLabel',sprintfc('%.1f', xtr))
Using
M_Swapped = M(end:-1:1);
M_Swapped(end) = 0
N_Swapped = N(end:-1:1)
I end up with the original plot! I feared that would happen.
I need to swap the points such that it gets plotted like this
Réponse acceptée
Alex Mcaulley
le 25 Mar 2019
N = flip(M);
N(end) = 0;
2 commentaires
John D'Errico
le 25 Mar 2019
So just swap N AND M. What is the problem? Use of flip will be efficient.
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Logical dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!