how to transfer output in new matrix

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
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
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
Thank you for your reply. But i want a matrix that contains the values of if statement and on rest indexes i want zero. I want a matrix of same size with elements of if statement and rest zeros
DGM
DGM le 8 Fév 2023
Modifié(e) : DGM 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.

Connectez-vous pour commenter.

Réponses (1)

DGM
DGM le 8 Fév 2023
Modifié(e) : DGM le 8 Fév 2023
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]
ans = 4×42
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 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 7 0 6 0 0 5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 6 0 5 0 0 4 0 0 0 0 0
% 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{:})
Speed is 0; Braking is inactive Speed is 0; Braking is inactive Speed is 0; Braking is inactive Speed is 2; Braking is inactive Speed is 2; Braking is inactive Speed is 2; Braking is inactive Speed is 3; Braking is inactive Speed is 3; Braking is inactive Speed is 3; Braking is inactive Speed is 4; Braking is inactive Speed is 4; Braking is inactive Speed is 4; Braking is inactive Speed is 5; Braking is inactive Speed is 5; Braking is inactive Speed is 6; Braking is inactive Speed is 6; Braking is inactive Speed is 6; Braking is inactive Speed is 7; Braking is inactive Speed is 7; Braking is inactive Speed is 7; Braking is active Speed is 6; Braking is inactive Speed is 6; Braking is active Speed is 5; Braking is inactive Speed is 5; Braking is inactive Speed is 5; Braking is active Speed is 4; Braking is inactive Speed is 4; Braking is inactive Speed is 4; Braking is inactive Speed is 5; Braking is inactive Speed is 5; Braking is inactive Speed is 5; Braking is active Speed is 3; Braking is inactive Speed is 3; Braking is active Speed is 2; Braking is inactive Speed is 2; Braking is inactive Speed is 2; Braking is active Speed is 1; Braking is inactive Speed is 1; Braking is inactive Speed is 1; Braking is active Speed is 0; Braking is inactive Speed is 0; Braking is inactive Speed is 0; Braking is inactive

3 commentaires

Thanks for your reply . But i want a (1,571618) matrix in which 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 . & now lets assume i apllied a brakes from interval (1,32354) to (1,32380) & my speed reduced gradually from 93 to 87 in that interval. so it must be indicicate reduction of Speed in these portion of the matrix.
DGM
DGM le 8 Fév 2023
Modifié(e) : DGM 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:
  1. "i want both the value when braking occurs and when is not."
  2. "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]
ans = 4×26
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 0 0 1 1 1 1 1 1 0 0 0 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 0 0 10 9 8 7 6 5 0 0 0 0 0 0 0 0 6 5 4 3 2 0 0 0 0 0 0 0 9 8 7 6 5 4 0 0 0 0 0 0 0 0 5 4 3 2 1 0 0 0 0 0
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.
sorry input is 565660

Connectez-vous pour commenter.

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by