how to transfer output in new matrix
Afficher commentaires plus anciens
I have a huge [1,565660] matrix.
I have used a FOR loop and an IF/ELSE statement to find some specific data. But I don’t know how to transfer the output into a new matrix of same size as I want to multiple the result later.
For example:Speed=[0,0,0,2,2,2,3,3,3,4,4,4,5,5,6,6,6,7,7,7,6,6,5,5,5,4,4,4,5,5,5,3,3,2,2,2,1,1,1,0,0,0] already have this matrix saved in my workspace.
I need those fprintf outputs as a matrix. How can I do it?
Please help.
for i = 1:length(speed)-1
if speed(1,i)>speed(1,i+1)
fprintf('%d: brake start %d\n %d: %d end \n', i,speed(i),(i+1),speed(i+1));
else
fprintf('index%d: no braking : speed%d\n',i,speed(i));
end
end
4 commentaires
Sarvesh Kale
le 8 Fév 2023
Modifié(e) : Sarvesh Kale
le 8 Fév 2023
Does this code help ?
b=[]; % matrix to store braking info
nb=[];% matrix to store no braking info
for i = 1:length(speed)-1
if speed(1,i)>speed(1,i+1)
a=[a;i,speed(i),(i+1),speed(i+1)]; % append to matrix row wise
else
b=[b;i,speed(i)]; % append to matrix row wise
end
end
a and b will contain the matrices you want
Luca Ferro
le 8 Fév 2023
Modifié(e) : Luca Ferro
le 8 Fév 2023
correct me if i misunderstood, you want to cycle through an array Speed, get values that match your condition from said array and store them in Brake?
if so:
brake=[];
for i = 1:length(speed)-1 %please don't use i as an index for loops, it's deprecated since it's very similar to 1
if speed(1,i)>speed(1,i+1)
fprintf('%d: brake start %d\n %d: %d end \n', i,speed(i),(i+1),speed(i+1));
brake=[brake speed(1,i)];
else
fprintf('index%d: no braking : speed%d\n',i,speed(i));
end
end
and then you lost me
Umang Babubhai Rabari
le 8 Fév 2023
"I need those fprintf outputs as a matrix."
"i want a matrix that contains the values of if statement and on rest indexes i want zero"
There are no output values being calculated in your if statement. That's why everyone is trying to guess what you want. What values do you want?
Do you want the values from speed when braking occurs?
Do you want the values from speed when no braking occurs?
Do you want a logical vector describing the braking state?
Do you want a bunch of text in an array?
If you have a huge dataset, why are you printing hundreds of thousands of lines of text to console? It only slows things down and you'll never be able to read it all anyway.
In any case, loops shouldn't be necessary.
% locations of braking start
state = [diff(speed)<0 false];
That gives a logical vector of all locations where the "braking start" message is produced. If you want some value from speed, use the logical vector to index into speed.
Réponses (1)
Here's my guess
speed = [0 0 0 2 2 2 3 3 3 4 4 4 5 5 6 6 6 7 7 7 6 6 5 5 5 4 4 4 5 5 5 3 3 2 2 2 1 1 1 0 0 0];
% locations of braking start
state = [diff(speed)<0 false];
% speed values before braking event
sp0 = zeros(size(speed));
sp0(state) = speed(state);
% speed values after braking event
sp1 = zeros(size(speed));
sp1(state) = speed(circshift(state,1));
% show what the results are
[speed; state; sp0; sp1]
% or if you want text output, use sprintf()/fprintf()
% pick whatever relevant values you want
statenames = {'inactive','active'};
outputelements = [num2cell(speed); statenames(state+1)];
fprintf('Speed is %d; Braking is %s\n',outputelements{:})
3 commentaires
Umang Babubhai Rabari
le 8 Fév 2023
If your input has 565660 elements, why does the output have 571618 elements? If that's a mistake and they're supposed to be the same length, then these two statements are contradictory:
- "i want both the value when braking occurs and when is not."
- "so when there are no brakes apply there must be 0 in the index"
Statement 1 is equivalent to the input. Statement 2 is equivalent to sp0.
The same thing, but with a more continuous input:
speed = [10 10 10 9 8 7 6 5 4 4 4 4 4 5 5 6 6 5 4 3 2 1 1 1 1 1 ];
% locations of braking start
state = [diff(speed)<0 false];
% speed values before braking event
sp0 = zeros(size(speed));
sp0(state) = speed(state);
% speed values after braking event
sp1 = zeros(size(speed));
sp1(state) = speed(circshift(state,1));
% show what the results are
[speed; state; sp0; sp1]
The only reason I'm concatenating the outputs is to make sure they're easier to read. You don't need to use them in this matrix format.
Umang Babubhai Rabari
le 8 Fév 2023
Catégories
En savoir plus sur Timetables dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!